Class member function does not accept another class as an argument

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.
Move the definition of Treasure before the definition of Man.
Thanks helios, it works if I move it. Is it possible for it to work without moving it though, with some sort of class prototype for example?
Would your solution work if a function of Treasure used Man as an argument?
I think there's a syntax called "forward declaration" which, in this case, would look like this: class Treasure;
There was a forward declaration in the example above. Perhaps it is because it was passed by reference and the Man class would need to know the implementation of the copy constructor for the Treasure class... Try passing it: const Treasure &.

On second thought, it would still need to know about the other method it calls. If you separate your declarations and definitions, then it would work.
Last edited on
Thanks Seymore, but what do you mean by separating the declarations and definitions? Do you mean I should not be using inline functions?
Thanks I'll see if I can make that work.
Topic archived. No new replies allowed.