solved  Pointer to class

j3lps (11)   Link to this post
Hi all, I'm trying to get a simple program to work, but have got stuck very early on, and can't figure out why.

So I have a header file which define 2 classes:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class CPlant
{
public:
	CPlant();
	CRoot* root_system;  // error C2143
	float stM;
	CPlant* PP;
};

class CRoot
{
public:
	CRoot(CPlant* PP);
	float get_C_conc(float vm);
	CPlant* PPlant;
};

And then the body which simply creates an object:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
int main(){
	CPlant MyObject;
	return 0;
}

CPlant::CPlant() 
{
	PP = this; 
	root_system = new CRoot(PP); 	
}

CRoot::CRoot(CPlant* PP)
{
	PPlant = PP;
}


However it won't compile as it produces errorC2143: syntax error : missing ';' before '*' where I have marked above. Can anyone tell me what's wrong with this?
Many thanks. J.
demosthenes2k8 (161)   Link to this post
The problem is that, up until that line, there's been no mention of CRoot. Try:

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

class CPlant
{
public:
	CPlant();
	CRoot* root_system;
	float stM;
	CPlant* PP;
};

class CRoot
{
public:
	CRoot(CPlant* PP);
	float get_C_conc(float vm);
	CPlant* PPlant;
};
j3lps (11)   Link to this post
Thanks - that works. How weird - I assumed a header file was not read in order... Cheers.
demosthenes2k8 (161)   Link to this post
Welcome, I was excited that I knew the answer! Welcome to http://en.wikipedia.org/wiki/Dependency_hell you have to make sure that there's actually SOME mention of that class before you use it.
firedraco (2623)   Link to this post
Basically all #include does is copy/paste the file into wherever you have the include.

This topic is archived - New replies not allowed.