#include<iostream>
using namespace std;
class StudentClass{
friend class Oopclass;
public:
StudentClass();
void setStudentData();
void printStudentScore();
StudentClass* next;
~StudentClass();
private:
string std_ID;
int sID, sMid, sFin, sOP, hw[3];
double sTotal;
};
class OopClass{
friend class StudentClass;
public:
OopClass();
void setOopData();
void printOopScore();
void queryOopScore();
void findOopAverageScore();
void addStudentData();
void deleteStudentData();
void updateStudentData();
~OopClass();
private:
string professor, subject;
int cSn;
double cSAvg;
StudentClass* cSAddr;
};
int main(int argc, char **argv){
OopClass Info[3];
Info[0].setOopData();
return 0;
}
StudentClass::StudentClass(){
}
void StudentClass(){
}
void StudentClass::setStudentData() {
}
StudentClass::~StudentClass(){
}
OopClass::OopClass(){
}
void OopClass::setOopData() {
cout << "Professor?" << endl;
cin >> professor;
cout << "Subject?" << endl;
cin >> subject;
cout << "How many students do you want to input ?" << endl;
int number = 0;
cin >> number;
cSAddr = new StudentClass;
//for(int i = 0; i < number; i ++) setStudentData();
}
OopClass::~OopClass(){
}
When I tired to write "cSAddr = new StudentClass;" ,it come up to
error about
" C:\Users\lypan\Documents\final.cpp In member function 'void
OopClass::setOopData()':
63 14 C:\Users\lypan\Documents\final.cpp [Error] expected
type-specifier before 'StudentClass'
63 14 C:\Users\lypan\Documents\final.cpp [Error] invalid conversion
from 'int*' to 'int' [-fpermissive]
63 14 C:\Users\lypan\Documents\final.cpp [Error] expected ',' or ';'
before 'StudentClass' "
And I have no idea why I do wrong, please tell me what I make mistake. Thx!
The only error here is that you didn't implement the constructors for your classes. But that should yield a linker error.
This probably isn't your code, but it might be that you don't include StudentClass.h and OopClass.h (or whatever) in the file where you define OopClass::setOopData().
EDIT:
void StudentClass()
you declare a function called StudentClass on line 46 - http://codepad.org/9DP6zpHU - you probably forgot to qualify it.
StudentClass::StudentClass(){
}
//THIS!
void StudentClass(){
}
void StudentClass::setStudentData() {
}
The error is you define a function with the same name as the class StudentClass:
void StudentClass(){
}
Remove that function and it should work better.
Related
#include <iostream>
#include <sstream>
#include <cstring>
using namespace std;
class Student {
int age;
char first_name[30];
char second_name[30];
int standard;
public:
void set_age(int a) {
age=a;
}
void set_standard(int b){
standard=b;
}
void set_last_name(char temp[]) {
strcpy(second_name,temp);
}
void get_age() {
cout<<age;
}
void get_last_name(){
cout<<second_name;
}
void get_first_name() {
cout<<first_name;
}
void get_standard(){
cout<<standard;
}
};
void Student::set_first_name(char temp[]){
strcpy(first_name,temp);
}
int main() {
int age, standard;
string first_name, last_name;
cin >> age >> first_name >> last_name >> standard;
Student st;
st.set_age(age);
st.set_standard(standard);
st.set_first_name(first_name);
st.set_last_name(last_name);
cout << st.get_age() << "\n";
cout << st.get_last_name() << ", " << st.get_first_name() << "\n";
cout << st.get_standard() << "\n";
cout << "\n";
cout << st.to_string();
return 0;
}
errors
Solution.cpp:35:6: error: no declaration matches ‘void Student::set_first_name(char*)’
void Student::set_first_name(char temp[]){
^~~~~~~
Solution.cpp:35:6: note: no functions named ‘void Student::set_first_name(char*)’
Solution.cpp:6:7: note: ‘class Student’ defined here
class Student {
^~~~~~~
Solution.cpp: In function ‘int main()’:
Solution.cpp:48:8: error: ‘class Student’ has no member named ‘set_first_name’; did you mean ‘get_first_name’?
st.set_first_name(first_name);
^~~~~~~~~~~~~~
get_first_name
Solution.cpp:49:31: error: no matching function for call to ‘Student::set_last_name(std::__cxx11::string&)’
st.set_last_name(last_name);
^
Solution.cpp:18:10: note: candidate: ‘void Student::set_last_name(char*)’
void set_last_name(char temp[]) {
You have provided a definition of the method, but the method is not declared in the class.
Add this line inside the Student class to provide a declaration:
void set_first_name(char temp[]);
you don't declare your function in your class
add this line to class body and the error will disappear
void set_first_name(char temp[]);
You have several issues:
cout cannot print void.
you haven't declared a function but you try to define it.
#include <iostream>
#include <sstream>
#include <cstring>
using namespace std;
class Student {
int age;
char first_name[30];
char second_name[30];
int standard;
public:
void set_age(int a) {
age=a;
}
void set_standard(int b){
standard=b;
}
void set_last_name(const char* temp) {
strcpy(second_name,temp);
}
void get_age() {
cout<<age;
}
void get_last_name(){
cout<<second_name;
}
void get_first_name() {
cout<<first_name;
}
void get_standard(){
cout<<standard;
}
void set_first_name(const char* temp){
strcpy(first_name,temp);
}
};
int main() {
int age, standard;
string first_name, last_name;
cin >> age >> first_name >> last_name >> standard;
Student st;
st.set_age(age);
st.set_standard(standard);
st.set_first_name(first_name.c_str());
st.set_last_name(last_name.c_str());
st.get_age();
st.get_last_name();
st.get_first_name();
st.get_standard();
cout << "\n";
//cout << st.to_string();
return 0;
}
You need to have something like void set_first_name(char temp[]) in the deceleration of your student class. Also, strings are not the same as char arrays and must be casted to that type. I recommend using strings in your class and not char arrays. This would be causing your last few errors.
I want to add to Oblivion's answer that you cannot cout first_name, second_name as well. First_name and second_name are pointers to the first element of the character array. Printing them would lead to just printing their addresses in the console.
You have to change these function declarations:
void get_last_name(){
cout<<second_name;
}
void get_first_name(){
cout<<first_name;
}
You need to add '\0' (end of string character) to the character arrays and then print it as a string. Check out how to do it by searching. You will learn a lot about working of character arrays and string.
So, for class, I have to write a program that simulates a horse race. It's our first major project involving classes. I'm stuck receiving the error mentioned in the subject. Now, I have struggled with pointers since they were introduced, so I have zero doubt this is where my overall problem lies. I've asked my professor for help, but he's not replying to my emails. I've also contacted friends, etc. and no one is getting back to me.
This is my header file (Horse.h):
#ifndef HORSE_H
#define HORSE_H
#include <string>
class Horse
{
private:
std::string name;
std::string jockey;
int maxSpeed;
int distanceTraveled;
int racesWon;
public:
Horse(std::string, std::string);
void runOneSecond(int);
void sendToGate();
void displayHorse (double);
};
#endif // HORSE_H
Here is my Horse.cpp:
#include <iostream>
#include <string>
#include <stdlib.h>
#include <time.h>
#include "Horse.h"
using namespace std;
Horse::Horse(string name, string jockey)
{
srand (time(NULL));
int maxSpeed = rand() % 30 + 1;
distanceTraveled = 0;
};
void Horse::runOneSecond(int maxSpeed)
{
srand (time(NULL));
distanceTraveled = rand() % maxSpeed;
};
void Horse::sendToGate()
{
distanceTraveled = 0;
};
void Horse::displayHorse(double raceDistance)
{
int percentage;
for (int i = 0; i < numberOfHorses; i++)
{
cout << "|";
}
};
And here is my main.cpp:
#include <iostream>
#include <string>
#include <cctype>
#include "Horse.h"
using namespace std;
int main()
{
double raceDistance = 0;
int numberOfHorses = 0;
char choice = 'Y';
string name;
string jockey;
cout << "Enter the number of horses for the race: ";
cin >> numberOfHorses;
Horse** horsePtr = new Horse* [numberOfHorses];
// Trouble section.
for (int i = 0; i < numberOfHorses; i++)
{
cout << "Fill in the name of the horse: ";
cin >> horsePtr[i]->name;
cout << "Fill in the name of the jockey: ";
cin >> horsePtr[i]->jockey;
}
cout << "How long should the race be (in meters): ";
cin >> raceDistance;
cout << endl;
cout << "Start!" << endl;
for (int i = 0; i < numberOfHorses; i++)
{
horsePtr[i]->sendToGate();
}
for (int i = 0; i < numberOfHorses; i++)
{
horsePtr[i]->displayHorse(raceDistance);
}
cout << "Show the next second of the race? ";
cin >> choice;
while(toupper(choice) == 'Y')
{
if (toupper(choice) == 'Y')
{
for (int i = 0; i < numberOfHorses; i++)
{
horsePtr[i]->runOneSecond(maxSpeed);
horsePtr[i]->displayHorse(raceDistance);
}
}
}
return 0;
}
Here is the error:
Horse.cpp: In member function ‘void Horse::displayHorse(double)’:
Horse.cpp:29:23: error: ‘numberOfHorses’ was not declared in this scope
for (int i = 0; i < numberOfHorses; i++)
^
In file included from main.cpp:4:0:
Horse.h: In function ‘int main()’:
Horse.h:8:17: error: ‘std::string Horse::name’ is private
std::string name;
^
main.cpp:25:25: error: within this context
cin >> horsePtr[i]->name;
^
In file included from main_dmj8t6.cpp:4:0:
Horse.h:9:17: error: ‘std::string Horse::jockey’ is private
std::string jockey;
^
main.cpp:27:25: error: within this context
cin >> horsePtr[i]->jockey;
^
main.cpp:55:35: error: ‘maxSpeed’ was not declared in this scope
horsePtr[i]->runOneSecond(maxSpeed);
^
You cannot access a private member of a class. In your example, your Horse class contains 5 private members:
class Horse
{
private:
std::string name;
std::string jockey;
int maxSpeed;
int distanceTraveled;
int racesWon;
public:
};
These private members can be accessed within the Horse class methods, and by any friend classes; however: nothing else can access them.
int main()
{
// Trouble section.
for (int i = 0; i < numberOfHorses; i++)
{
cout << "Fill in the name of the horse: ";
cin >> horsePtr[i]->name; <--
cout << "Fill in the name of the jockey: ";
cin >> horsePtr[i]->jockey; <--
}
}
On the two lines marked, you are attempting to access the private members of Horse and the compiler won't allow it.
Consider making those variables public to Horse, or providing setter functions for them:
class Horse
{
private:
std::string name;
std::string jockey;
int maxSpeed;
int distanceTraveled;
int racesWon;
public:
void SetName(std::string s) { name = s; }
void SetJockey(std::string s) { jockey = s; }
};
int main()
{
std::string jockeyName;
std::cout << "Enter a name for the jockey: ";
std::cin >> jockeyName;
Horse* h = new Horse;
h->SetJockey(jockeyName);
}
You are providing a public constructor for Horse that takes two std::string though (neither which you are using) so you could just pass the relevant information to the Horse:
Horse::Horse(std::string n, std::string j) : name(n), jockey(j)
{
// Other things...
}
int main()
{
Horse* h = new Horse("Phar Lap", "Jim Pike");
}
Note that my two examples are just matching your current code. Your arrays should be replaced by std::vector and pointers, if required, should be replaced by std::unique_ptr (or std::shared_ptr, whichever suits your needs).
I get three errors while i try to compile this program.
I am expecting the following output
OFF-ctor Enter 0/1: 0 already OFF Enter 0/1: 1
going from OFF to ON ON-ctor dtor-OFF Enter 0/1: 1
already ON Enter 0/1: 0 going from ON to OFF OFF-ctor
dtor-ON Enter 0/1: 1 going from OFF to ON ON-ctor
dtor-OFF Enter 0/1: 0 going from ON to OFF OFF-ctor
dtor-ON Enter 0/1: 0 already OFF Enter 0/1:
Following is the program
#include <iostream>
using namespace std;
class Machine
{
class State *current;
public:
Machine();
void setCurrent(State *s)
{
current = s;
}
void on();
void off();
};
class State
{
public:
virtual void on(Machine *m)
{
cout << " already ON\n";
}
virtual void off(Machine *m)
{
cout << " already OFF\n";
}
};
void Machine::on()
{
current->on(this);
}
void Machine::off()
{
current->off(this);
}
class ON: public State
{
public:
ON()
{
cout << " ON-ctor ";
};
~ON()
{
cout << " dtor-ON\n";
};
void off(Machine *m);
};
class OFF: public State
{
public:
OFF()
{
cout << " OFF-ctor ";
};
~OFF()
{
cout << " dtor-OFF\n";
};
void on(Machine *m)
{
cout << " going from OFF to ON";
m->setCurrent(new ON());
delete this;
}
};
void ON::off(Machine *m)
{
cout << " going from ON to OFF";
m->setCurrent(new OFF());
delete this;
}
Machine::Machine()
{
current = new OFF();
cout << '\n';
}
int main()
{
void(Machine:: *ptrs[])() =
{
Machine::off, Machine::on//Error2->invalid use of non-static member function 'void Machine::off()'
//Error3->invalid use of non-static member function 'void Machine::on()'
};
Machine fsm;
int num;
while (1)
{
cout << "Enter 0/1: ";
cin >> num;
(fsm. *ptrs[num])(); //Error1->expected unqualified-id before '*' token
}
}
The code was taken from sourcemaking.com under state design pattern.
I ran it in eclipse and linux g++.
Kindly help.
To get a pointer to a member function, you need to use an & (even though it's optional for getting a pointer to a non-member function): &Machine::off, &Machine::on
For the other, you need to realize that .* is a single token, so you need to remove the space between the two characters: (fsm.*ptrs[num])();
void (Machine::*ptrs[])() =
{
&Machine::off, // note: explicit &
&Machine::on
};
Machine fsm;
int num;
while (1)
{
cout << "Enter 0/1: ";
cin >> num;
(fsm.*ptrs[num])(); // note: no space between . and *
}
That still leaves the following warnings:
try.cc:19:22: warning: unused parameter 'm' [-Wunused-parameter]
try.cc:23:22: warning: unused parameter 'm' [-Wunused-parameter]
try.cc: In member function 'virtual void OFF::on(Machine*)':
try.cc:68:20: warning: deleting object of polymorphic class type 'OFF' which has non-virtual destructor might cause undefined behaviour [-Wdelete-non-virtual-dtor]
try.cc: In member function 'virtual void ON::off(Machine*)':
try.cc:76:14: warning: deleting object of polymorphic class type 'ON' which has non-virtual destructor might cause undefined behaviour [-Wdelete-non-virtual-dtor]
I'm getting the following error upon compiling:
Compilation started at Wed Oct 5 03:05:32
|make -k proj1
|g++ proj1.cc -o proj1
|proj1.cc: In function ‘int main()’:
|proj1.cc:75:13: error: no matching function for call to ‘getData()’
|proj1.cc:75:13: note: candidate is:
|proj1.cc:46:6: note: template<class T> void getData(Vector<T>&, int&)
|proj1.cc:80:16: error: no matching function for call to
‘computeSum()’
|proj1.cc:80:16: note: candidate is:
|proj1.cc:28:6: note: template<class T> void computeSum(Vector<T>,
int, T&, bool&)
|proj1.cc:83:9: error: ‘success’ was not declared in this scope
|proj1.cc:84:27: error: ‘total’ was not declared in this scope
make: *** [proj1] Error 1
Compilation exited abnormally with code 2 at Wed Oct 5 03:05:33
Am I simply not calling my template
#include <std_lib_facilities.h>
#include <vector>
#include <string>
#include <iostream>
class T
{
public:
void computeSum(vector<T> in, int n, T &out, bool &success);
void getData(vector<T> &data, int &howMany);
};
template <class T>
// void computeSum(vector<T> data, int howMany, T& out, bool& success)
void computeSum(vector<T> data, int n, T &out, bool &success)
{
if (n < data.size()){
success = true;
int i = 0;
while (i<n){
out = out + data[i];
++i;
}
} else {
success = false;
cerr << "You can not request to sum up more numbers than there are.\n";
}
}
template <class T>
void getData(vector<T> &data, int &howMany)
{
cout << "Please insert the data:\n";
T n;
do{
cin >> n;
data.push_back(n);
} while (n<howMany);
cout << "This vector has " << data.size() << " numbers.\n";
}
void offerHelp()
{
cout << "Do you want help? ";
string help;
cin >> help;
if (help == "n" || help == "no"){
cout << endl;
}else{
cout << "Enter your data. Negative numbers will be added as 0. Ctrl-D to finish inputing values.\n";
}
}
int main()
{
offerHelp();
getData();
cout << "How many numbers would you like to sum?";
int howMany;
cin >> howMany;
computeSum();
if (success = true) {
cout << "The sum is " << total << endl;
} else {
cerr << "Oops, an error has occured.\n";
}
cout << endl;
return 0;
}
Your function is declared and defined as:
void getData(Vector&, int&)
You are calling it as:
getData();
Clearly, the compiler cannot find the function which takes no parameteres and hence the no mathching function error.
Same is the case for computeSum().
There are a host of other errors as well like success and total are two variables which are being accessed in main but not declared anywhere inside the main.
You are forgetting to pass the parameters to your functions e.g., :
void computeSum(vector<T> data, int n, T &out, bool &success)
computeSum();
Apparently the function signatures do not match. Also your class T is not declared as a template class. I think this is what you originally intended. The functions computeSum and getData do not implement the public member functions of the class.
here is the code with the problem:
#ifndef WEAPONS_H_INCLUDED
#define WEAPONS_H_INCLUDED
#include "Battleship header.h"
void Player::torpedo(string enemyEvtTracker[][10], string myhitboard[][10])
{
string bcn; //board column number
cout << "Enter board column number (1-9): "; cin >> bcn; flush();
if(bcn!="1"&&bcn!="2"&&bcn!="3"&&bcn!="4"&&bcn!="5"&&bcn!="6"&&bcn!="7"&&bcn!="8"&&bcn!="9")
{cout <<endl<< "That is not a valid number.";}
return;
}
#endif // WEAPONS_H_INCLUDED
here is the class Player:
#ifndef BATTLESHIPPLAYERCLASS_H_INCLUDED
#define BATTLESHIPPLAYERCLASS_H_INCLUDED
using namespace std;
class Player // define a class for a player
{
void torpedo(string enemyEvtTracker[10][10], string myhitboard[10][10]);
void cannon();
void scatter();
void menu();
friend int main(int, char*[]); //Let main access the protected members
friend int routine_END(void);
public:
void displaydata()
{cout << money << endl << gunpowder << endl << ammo_cannon << endl << ammo_4hit << endl;}
string savename;
int gunpowder;
int ammo_cannon;
int ammo_4hit; string gun_4;
int ammo_scatter; string gun_s;
int money;
Player(string name){money=18000; gunpowder=100;ammo_cannon=20; ammo_4hit=0; ammo_scatter=0; gun_4="OFF"; gun_s="OFF";playername=name;} //Define the constructor
void simplegame(void) {gunpowder=99999999; ammo_cannon=999999999; ammo_scatter=4; gun_s="ON";}
void getname(string *playername, int option)
{
if (option==1)
{cout << "Enter your name here player 1:"; cin >> *playername;}
else {cout << "Enter your name here player 2:"; cin >> *playername;}
}
string playername;
char mainRowGuess;
int check_transaction (int mymoney, int moneyowed)
{
if (mymoney-moneyowed<<0) {return 0;}
else {return 1;}
}
void change_Answer_to_number(char row,int* outputRow)
{
if (row=='A'||row=='a'){*outputRow =1;}
else if (row=='B'||row=='b'){*outputRow =2;}
else if (row=='C'||row=='c'){*outputRow =3;}
else if (row=='D'||row=='d'){*outputRow =4;}
else if (row=='E'||row=='e'){*outputRow =5;}
else if (row=='F'||row=='f'){*outputRow =6;}
else if (row=='G'||row=='g'){*outputRow =7;}
else if (row=='H'||row=='h'){*outputRow =8;}
else if (row=='I'||row=='i'){*outputRow =9;}
else {*outputRow = 0;}
}
void changeCharToNumber(char row, int* outputRow)
{
if (row=='1'){*outputRow=1;}
else if (row=='2'){*outputRow=2;}
else if (row=='3'){*outputRow=3;}
else if (row=='4'){*outputRow=4;}
else if (row=='5'){*outputRow=5;}
else if (row=='6'){*outputRow=6;}
else if (row=='7'){*outputRow=7;}
else if (row=='8'){*outputRow=8;}
else if (row=='9'){*outputRow=9;}
else {cout << "Unexpected Error." << endl; *outputRow=0;}
}
char airRowStart; char airColStart; char aircraftDirec;
char destRowStart; char destColStart; char destroyerDirec;
char subRowStart; char subColStart; char subDirec;
char patrolStart; char patrolDirec;
/// START MENU FUNCTION
void error_money(void) {cout << "Not enough money!";}
char startRowAircraftCarrier;
char startRowDestroyer;
char startRowSub;
char startRowPatrolBoat;
friend int routine_END (void);
friend void menu (int* gunpowder, int* ammo_cannon, int* ammo_4hit, int* ammo_scatter, int* money, string* gun_4, string* gun_s);
};
#endif // BATTLESHIPPLAYERCLASS_H_INCLUDED
and this generates the following build log...
-------------- Build: Debug in Advanced Battleship Obj
[ 50.0%] Compiling: main.cpp
C:\Advanced_Battleship_Revised_5111\main.cpp:32:
warning: ignoring #pragma comment In
file included from
C:\Advanced_Battleship_Revised_5111\/imputoutput.h:9,
from C:\Advanced_Battleship_Revised_5111\/Battleship
header.h:3,
from C:\Advanced_Battleship_Revised_5111\main.cpp:25:
C:\Advanced_Battleship_Revised_5111\/BattleshipPlayerClass.h:74:
warning: 'int routine_END()' is
already a friend of class 'Player'
C:\Advanced_Battleship_Revised_5111\/BattleshipPlayerClass.h:
In member function 'int
Player::check_transaction(int, int)':
C:\Advanced_Battleship_Revised_5111\/BattleshipPlayerClass.h:33:
warning: suggest parentheses around
'-' inside '<<' In file included from
C:\Advanced_Battleship_Revised_5111\main.cpp:27:
C:\Advanced_Battleship_Revised_5111\/BattleshipMenu.h:
In member function 'void
Player::menu()':
C:\Advanced_Battleship_Revised_5111\/BattleshipMenu.h:118:
warning: label 'GUNPOWDER_MENU_1'
defined but not used
C:\Advanced_Battleship_Revised_5111\/BattleshipMenu.h:166:
warning: label
'CIN_WEAPON_OPTION_SCATTER_CANNON'
defined but not used In file included
from
C:\Advanced_Battleship_Revised_5111\main.cpp:30:
C:\Advanced_Battleship_Revised_5111\/weapons.h:
At global scope:
> C:\Advanced_Battleship_Revised_5111\/weapons.h:5: error: declaration of
'enemyEvtTracker' as multidimensional
array must have bounds for all
dimensions except the first
C:\Advanced_Battleship_Revised_5111\/weapons.h:5: error: expected ')' before ',' token
C:\Advanced_Battleship_Revised_5111\/weapons.h:5: error: expected constructor,
destructor, or type conversion before
',' token
C:\Advanced_Battleship_Revised_5111\/weapons.h:5: error: expected constructor,
destructor, or type conversion before
'myhitboard' Process terminated with
status 1 (0 minutes, 1 seconds) 4
errors, 5 warnings
Player::torpedo(string enemyEvtTracker[10][10], string myhitboard[10][10])
{
//..
}
This is the definition of the member function. Where is the declaration?
My guess is that in the declaration you've not mentioned the size of the array, as you did in the definition. It seems you've written simply enemyEvtTracker [][]? See the definition of Player class, and verify how you've declared torpedo member function in it.