Hi everyone.
I am trying to make a very basic game as an exercise in classes, and I have got stuck.
To recreate my problem I have written 2 classes, one for a man, and another for treasure. In main() I first instantiate 2 'Man' classes, called man1 and man2; and one 'Treasure' class called gold. man1 then takes all man2's gold using "man1.findMan(man2)". This works. Finally I attempted to get man1 to 'find' the instance of gold, and add its value to his own using "man1.findTreasure(gold)". This does not work.
My code:
___________________
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
|
#include <iostream>
using namespace std;
class Treasure;
class Man {
private:
int health;
int gold;
public:
Man(int h, int g) {
health = h;
gold = g;
}
int getHealth() {
return health;
}
int getGold() {
return gold;
}
void setGold(int g) {
gold=g;
}
void findMan(Man& foundWhat) {
gold=+ foundWhat.getGold();
foundWhat.setGold(0);
return;
}
void findTreasure(Treasure foundWhat) {
gold=+ foundWhat.getValue();
}
void report() {
cout << "Man's health =\t" << health << "." << endl;
cout << "Man's gold =\t" << gold << "." << endl;
return;
}
};
class Treasure {
private:
int value;
public:
Treasure(int v) {
value = v;
}
int getValue() {
return value;
}
};
int main() {
Man man1(10, 0);
Man man2(10,5);
Treasure gold(10);
man1.report();
man2.report();
cout << "Man1 finds man2, steals his gold!" << endl;
man1.findMan(man2);
man1.report();
man2.report();
cout << "man1 finds gold!" << endl;
man1.findTreasure(gold);
man1.report();
man2.report();
return 0;
}
|
____________________
I get the following errors:
____________________
c:\documents and settings\thomas\my documents\visual studio 2008\projects\classes experiment\classes experiment\src.cpp(30) : error C2027: use of undefined type 'Treasure'
1> c:\documents and settings\thomas\my documents\visual studio 2008\projects\classes experiment\classes experiment\src.cpp(4) : see declaration of 'Treasure'
1>c:\documents and settings\thomas\my documents\visual studio 2008\projects\classes experiment\classes experiment\src.cpp(30) : error C2228: left of '.getValue' must have class/struct/union
___________________
I'm not very experienced in C++ so it could be something simple.
After repeated attempts to get this to work, I have had no luck. If anyone can see what it is I'm doing wrong then I would really appreciate the help.
I'm using Visual C++ 2008 Express Edition.