Problem with classes

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class one {
	public:
		void test(two t) {

		}
};

class two {
	public:
		void testt(one o) {

		}
};

int main() {
	return 0;
}


this gives the following error in visual c++ :
 
(3)error C2061: syntax error : identifier 'two'


how do i fix such a thing.
Last edited on
Need to use incomplete class def.
Add following to top of file (before class one definition)

class two;



gives me this error :

1
2
test.cpp(7): error C2027: use of undefined type 'two'
test.cpp(1) : see declaration of 'two'


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class two;

class one {
	public:
		void test(two t) {

		}
};

class two {
	public:
		int b;
		
		void testt(one o) {

		}
};

int main() {
	return 0;
}
Last edited on
You're redefining two in line 10. Erase the 'class' keyword, so that it simply stays
1
2
3
4
5
6
7
8
two {
	public:
		int b;
		
		void testt(one o) {

		}
};
If you pass by pointer or reference, the complete type will not be required. Try void test(const two & t) {.
it still doesnt work ..
If you ignore wasabi's post and do what moorecm suggested it will compile.

EDIT: you'll need to move the body of one::test() to below class two's declaration also.
Last edited on
The following works. 2 changes:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class two;  // change 1 : incomplete class def.

class one
{
  public:
    void test( two& t ) // change 2 : reference type (can not be value type)
    {}
};

class two
{
  public:
    void testt( one o )
    {}
};

Topic archived. No new replies allowed.