Some (beginners) trouble

Hi everybody,
I am learning C++, and I ran into some trouble. But I can't figure out exactly what it is I am doing wrong.
I have included my code, and the list of compiler errors. (There are 29 of them, but I suspect they are mostly related to one major error).

Thanks for your help in advance!!




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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
#include "stdafx.h" 

#include <iostream> 

using namespace std ;


class tree 
{
public: 
	
	tree(int age, int sugar, int leaves);  		//constuctor 
	tree();
	~tree();

	int getage(); 
	void setage(int age);
	int getsugar() ;
	void setsugar (int sugar) ;
	int getleaves() ;
	void setleaves (int leaves) ;
	void growleave (int n) ;
	
	void dfosy (int n)	;	//dag fotosynthese  
	void nfosy (int n)	;	//nacht fotosynthese

	void showgrowspeed ();
			

protected:  
	int	itsage ;
	int itssugar ;
	int itsleaves ;
	int growspeed ;	//virtual
	int maxleaves (50) ; //constant? 
}

tree::tree (int age, int sugar, int leaves) 
{
	itsage = age ;
	itssugar = sugar ;
	itsleaves = leaves ;
}

tree::tree () 
{
	itsage = 1 ;
	itssugar = 5 ;
	itsleaves = 1 ;
}

int tree::getage()
	{
		return itsage ;
	} 

void tree::setage(int age) 
	{
		itsage = age ;
	} 

int tree::getsugar()
	{
		return itssugar ;
	} 

void tree::setsugar(int sugar) 
	{
		itssugar = sugar ;
	} 

int tree::getleaves()
	{
		return itsleaves ;
	} 

void tree::setleaves(int leaves) 
	{
		itsleaves = leaves ;
	} 

void tree::growleave (int n) 
	{
		while (n != 0) 
		{
			itssugar = itssugar -5 ;
			itsleaves++ ;
			n-- ;
		}
	}

void tree::dfosy (int n) 
	{
		while (n != 0)		//geen +n (als n >1)gebruikt, ivm toevoegen functies.  
		{ 
			itssugar = itssugar + itsleaves ; 
			n-- ; 
		}
	} 

void tree::nfosy (int n) 
	{
		while (n != 0)		//geen +n (als n >1)gebruikt, ivm toevoegen functies.  
		{ 
			itssugar = itssugar - itsleaves ;
			n-- ;
		}
	}
void tree::showgrowspeed ()
{
	cout << "The growspeed is: " << growspeed << ". " ;
} 



class oak : public tree			//afleiding eik klasse 
{
public: 
		
protected: 
	int growspeed = 2 ;
} 

class maple : public tree		//afleiding esdoorn klasse 
{ 
public: 
		
protected:
	int growspeed = 3 ;
} 

class ash : public tree		 //afleiding es klasse 
{ 
public: 
	
protected:
	int growspeed = 2 ;
} 

;
int main () 
{
	int choise ;
	cout << "Please enter a number to create a tree. \nPress 1 to create an Oak tree \nPress 2 to create a Maple tree \nPress 3 to create an Ash tree \n" ;
	cin >> choise ;
		
	if (choise == 1)  
	{
		oak *ptree01 = new oak ;
		cout << "\nTree 1, Oak, succesfully created.\n\n" ;
	}

	else if (choise == 2) 
	{
		maple *ptree01 = new maple ;
		cout << "\nTree 1, Maple, succesfully created.\n\n" ;
	}

	else if (choise == 3) 
	{ 
		ash *ptree01 = new ash ;
		cout << "\nTree 1, Ash, succesfully created.\n\n" ;
	}

	else 
	{
		cout << "\nNo valid input received.\n" ; 
	} 
	
	cout << "Tree 1 is " << *ptree01->getage() << " years old.\n" ;
	
	int treechoise ;
	int menuchoise ;
	int noa ;		//number of actions 
	bool menu = true ;

	while (menu) 
	{ 
		cout << "\nPlease select a tree (0, 1 or 2)\n" ;
		cin >> treechoise ;
		cout << "\nplease select an event\n" << "Press 1 to see sugar level\nPress 2 to see number of leaves\nPress 3 to grow a new leaf\nPress 4 to start photosynthesis\nPress 5 to start night mode\nPress 6 to see the grow speed\nPress 7 to see the age\nPress 8 to exit.\n" ;
		
		if (menuchoise == 1)  
		{
			cout << "\nThe tree has " << (*ptree01).getsugar() << " sugar.\n" ;
			break;
		} 
		else if (menuchoise == 2) 
		{
			cout << "\nThe tree has " <<*ptree01->getleaves() << " leaves.\n" ;
			break;
		}
		else if (menuchoise == 3) 
		{
			cout << "\nGrowing a new leave. \n" ;
			*ptree01->growleave(1) ;
			break ; 
		}
		else if (menuchoise == 4) 
		{
			cout << "\nStarting photosynthesis.  \n" << "How many times should photosynthesis take place?\n" ;
			cin >> noa ;
			*ptree01->dfosy(noa) ;
			break ;
		}
		else if (menuchoise == 5)
		{
			noa = 0 ;
			cout << "\nStarting night.  \n" << "How many night should take place?\n" ;
			cin >> noa ;
			*ptree01->nfosy(noa) ;
			break ; 
		}
		else if (menuchoise == 6)
		{		
			cout << "\nThe growspeed is " ;
			*ptree01->showgrowspeed() ;
			cout << ".\n" ;
			break ; 
		}
		else if (menuchoise == 7)
		{
			cout << "\nThe tree is " << ptree01->getage << " years old.\n";
			break ; 
		}
		else 
		{
			cout << "\nExiting\n" ;
			menu = false ;
		}
	} 
	
return (0) ;
}


path(39) : error C2059: syntax error : 'constant'
path(43) : error C2533: 'tree::{ctor}' : constructors not allowed a return type
path(125) : error C2864: 'oak::growspeed' : only static const integral data members can be initialized within a class
path(128) : error C2236: unexpected 'class' 'maple'. Did you forget a ';'?
path(128) : error C2143: syntax error : missing ';' before ':'
path(128) : error C2059: syntax error : ':'
path(128) : error C2059: syntax error : 'public'
path(129) : error C2143: syntax error : missing ';' before '{'
path(129) : error C2447: '{' : missing function header (old-style formal list?)
path(141) : error C2864: 'ash::growspeed' : only static const integral data members can be initialized within a class
path(159) : error C2065: 'maple' : undeclared identifier
path(159) : error C2065: 'ptree01' : undeclared identifier
path(159) : error C2061: syntax error : identifier 'maple'
path(174) : error C2065: 'ptree01' : undeclared identifier
path(174) : error C2227: left of '->getage' must point to class/struct/union/generic type
type is ''unknown-type''
path(189) : error C2065: 'ptree01' : undeclared identifier
path(189) : error C2228: left of '.getsugar' must have class/struct/union
type is ''unknown-type''
path(194) : error C2065: 'ptree01' : undeclared identifier
path(194) : error C2227: left of '->getleaves' must point to class/struct/union/generic type
type is ''unknown-type''
path(200) : error C2065: 'ptree01' : undeclared identifier
path(200) : error C2227: left of '->growleave' must point to class/struct/union/generic type
type is ''unknown-type''
path(207) : error C2065: 'ptree01' : undeclared identifier
path(207) : error C2227: left of '->dfosy' must point to class/struct/union/generic type
type is ''unknown-type''
path(215) : error C2065: 'ptree01' : undeclared identifier
path(215) : error C2227: left of '->nfosy' must point to class/struct/union/generic type
type is ''unknown-type''
path(221) : error C2065: 'ptree01' : undeclared identifier
path(221) : error C2227: left of '->showgrowspeed' must point to class/struct/union/generic type
type is ''unknown-type''
path(227) : error C2065: 'ptree01' : undeclared identifier
path(227) : error C2227: left of '->getage' must point to class/struct/union/generic type
type is ''unknown-type''
Remember to finish off your classes with semi-colons.
hi undeads i'm new to this website too.
First of all in your class declaration you should write:

1
2
3
//...
    const int maxleaves;
//... 


and then initialize it to a value of your choice in the constructors
like this:

1
2
3
4
5
6
tree::tree (int age, int sugar, int leaves) : maxleaves(50)
{
	itsage = age ;
	itssugar = sugar ;
	itsleaves = leaves ;
}


Second the derived classes oak , ash & maple
shouldn't contain the growspeed variable because it is
inheritated from the tree class instead you should
define a constructor that set growspeed to your desired value


finally you have a mistake in line 170
you should replace it with:

cout << "Tree 1 is " << ptree01->getage() << " years old.\n" ;

you have similar mistakes in lines 190, 196, 203, 211, 217
also don't forget to delete the memory occupied by ptree01

that's for a start!
make those modifications and
then send me your results
Last edited on
Hi, thanks for your help!!
I reduced the number of errors from 29 to 18 (and the 18 remaining all look similar)
I tried several ways to acces the data using a pointer ( ((ptree01).getsugar()) ptree01->getleaves() ptree01.growleave() ; )
But they all produce an error. So maybe the way I appoint my pointer is wrong.
Again I have attached my code, and the errors I get.


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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
class tree 
{
public: 
	
	tree(int age, int sugar, int leaves);  		//constuctor 
	tree();
	~tree();

	int getage(); 
	void setage(int age);
	int getsugar() ;
	void setsugar (int sugar) ;
	int getleaves() ;
	void setleaves (int leaves) ;
	void growleave (int n) ;
	
	void dfosy (int n)	;	//dag fotosynthese  
	void nfosy (int n)	;	//nacht fotosynthese

	void showgrowspeed ();
			

protected:  
	int	itsage ;
	int itssugar ;
	int itsleaves ;
	int growspeed ;	//virtual
	const int maxleaves ; //constant? 
} ;

tree::tree (int age, int sugar, int leaves) : maxleaves (50)
{
	itsage = age ;
	itssugar = sugar ;
	itsleaves = leaves ;
}

tree::tree ()  : maxleaves (50)
{
	itsage = 1 ;
	itssugar = 5 ;
	itsleaves = 1 ;
}


class oak : public tree			//afleiding eik klasse 
{
public: 
	oak() ;						//afgeleide constructor 
	~oak() ;
} ;

oak::oak () 
{ 
	int growspeed = 2 ;
} ;


class maple : public tree		//afleiding esdoorn klasse 
{ 
public:
	maple () ;					//afgeleide constructor 
	~maple () ;
} ;

maple::maple ()
{
	int growspeed = 3 ;
} ;


class ash : public tree			//afleiding es klasse 
{ 
public:
	ash () ;						//afgeleide constructor 
	~ash () ;
} ;	

ash::ash() 
{
	int growspeed = 2 ;
} ;


int tree::getage()
	{
		return itsage ;
	} 

void tree::setage(int age) 
	{
		itsage = age ;
	} 

int tree::getsugar()
	{
		return itssugar ;
	} 

void tree::setsugar(int sugar) 
	{
		itssugar = sugar ;
	} 

int tree::getleaves()
	{
		return itsleaves ;
	} 

void tree::setleaves(int leaves) 
	{
		itsleaves = leaves ;
	} 

void tree::growleave (int n) 
	{
		while (n != 0) 
		{
			itssugar = itssugar -5 ;
			itsleaves++ ;
			n-- ;
		}
	}

void tree::dfosy (int n) 
	{
		while (n != 0)		//geen +n (als n >1)gebruikt, ivm toevoegen functies.  
		{ 
			itssugar = itssugar + itsleaves ; 
			n-- ; 
		}
	} 

void tree::nfosy (int n) 
	{
		while (n != 0)		//geen +n (als n >1)gebruikt, ivm toevoegen functies.  
		{ 
			itssugar = itssugar - itsleaves ;
			n-- ;
		}
	}
void tree::showgrowspeed ()
{
	cout << "The growspeed is: " << growspeed << ". " ;
} 



;
int main () 
{
	int choise ;
	cout << "Please enter a number to create a tree. \nPress 1 to create an Oak tree \nPress 2 to create a Maple tree \nPress 3 to create an Ash tree \n" ;
	cin >> choise ;
		
	if (choise == 1)  
	{
		oak *ptree01 = new oak ;
		cout << "\nTree 1, Oak, succesfully created.\n\n" ;
	}

	else if (choise == 2) 
	{
		maple *ptree01 = new maple ;
		cout << "\nTree 1, Maple, succesfully created.\n\n" ;
	}

	else if (choise == 3) 
	{ 
		ash *ptree01 = new ash ;
		cout << "\nTree 1, Ash, succesfully created.\n\n" ;
	}

	else 
	{
		cout << "\nNo valid input received.\n" ; 
	} 
	
	cout << "Tree 1 is " << ptree01->getage() << " years old.\n" ;
	
	int treechoise ;
	int menuchoise ;
	int noa ;		//number of actions 
	bool menu = true ;

	while (menu) 
	{ 
		cout << "\nPlease select a tree (0, 1 or 2)\n" ;
		cin >> treechoise ;
		cout << "\nplease select an event\n" << "Press 1 to see sugar level\nPress 2 to see number of leaves\nPress 3 to grow a new leaf\nPress 4 to start photosynthesis\nPress 5 to start night mode\nPress 6 to see the grow speed\nPress 7 to see the age\nPress 8 to exit.\n" ;
		
		if (menuchoise == 1)  
		{
			cout << "\nThe tree has " << ((ptree01).getsugar()) << " sugar.\n" ;
			break;
		} 
		else if (menuchoise == 2) 
		{
			cout << "\nThe tree has " <<ptree01->getleaves() << " leaves.\n" ;
			break;
		}
		else if (menuchoise == 3) 
		{
			cout << "\nGrowing a new leave. \n" ;
			ptree01->growleave(1) ;
			break ; 
		}
		else if (menuchoise == 4) 
		{
			cout << "\nStarting photosynthesis.  \n" << "How many times should photosynthesis take place?\n" ;
			cin >> noa ;
			ptree01->dfosy(noa) ;
			break ;
		}
		else if (menuchoise == 5)
		{
			noa = 0 ;
			cout << "\nStarting night.  \n" << "How many night should take place?\n" ;
			cin >> noa ;
			ptree01->nfosy(noa) ;
			break ; 
		}
		else if (menuchoise == 6)
		{		
			cout << "\nThe growspeed is " ;
			ptree01->showgrowspeed() ;
			cout << ".\n" ;
			break ; 
		}
		else if (menuchoise == 7)
		{
			cout << "\nThe tree is " << ptree01->getage << " years old.\n";
			break ; 
		}
		else 
		{
			cout << "\nExiting\n" ;
			menu = false ;
			delete ptree01 ;
		}
	} 
	
return (0) ;
}




path(190) : error C2065: 'ptree01' : undeclared identifier
path(190) : error C2227: left of '->getage' must point to class/struct/union/generic type
type is ''unknown-type''
path(205) : error C2065: 'ptree01' : undeclared identifier
path(205) : error C2228: left of '.getsugar' must have class/struct/union
type is ''unknown-type''
path(210) : error C2065: 'ptree01' : undeclared identifier
path(210) : error C2227: left of '->getleaves' must point to class/struct/union/generic type
type is ''unknown-type''
path(216) : error C2065: 'ptree01' : undeclared identifier
path(216) : error C2227: left of '->growleave' must point to class/struct/union/generic type
type is ''unknown-type''
path(223) : error C2065: 'ptree01' : undeclared identifier
path(223) : error C2227: left of '->dfosy' must point to class/struct/union/generic type
type is ''unknown-type''
path(231) : error C2065: 'ptree01' : undeclared identifier
path(231) : error C2227: left of '->nfosy' must point to class/struct/union/generic type
type is ''unknown-type''
path(237) : error C2065: 'ptree01' : undeclared identifier
path(237) : error C2227: left of '->showgrowspeed' must point to class/struct/union/generic type
type is ''unknown-type''
path(243) : error C2065: 'ptree01' : undeclared identifier
path(243) : error C2227: left of '->getage' must point to class/struct/union/generic type
type is ''unknown-type''
path(250) : error C2065: 'ptree01' : undeclared identifier
path(250) : error C2541: 'delete' : cannot delete objects that are not pointers
A lot of the definitions for your member functions have semi-colons that shouldn't be there e.g. lines 56, 69, 82 and some more...

Also, any pointers created inside the if statement code block will not exist outside of the code block; it will go out of scope.
mcleano is right.ptree01 goes out of scope once the program exit the if statement.
I suggest you declare a pointer to a base class globaly (so that it doesn't go out of scope)
and assign the value of ptree01 to it.
Try this and let me nknow about your results!
Last edited on
Thanks for your help!!
I reduced the number of errors to 1. But I have no idea how to solve this one.

I removed several semicolens (;) but the compiler started complaining, so I put some of them back. (If there are wrong semicolens again, Ill post what happens if I remove them (the exact compile error). Maybe I need to set the options of my compiler different.)

I have been messing with global pointers etc. But I thought it wasn't going to be very "C++ like". (orderly functions instead of one big mess)
So I created a new function in my main class (tree) which returns a pointer to a new object. (Is this a good idea?)
And the calling of this function is where I am stuck now.


Ps. (Almost) Happy new year!!





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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
#include "stdafx.h" 

#include <iostream> 

using namespace std ;


class tree 
{
public: 
	
	tree(int age, int sugar, int leaves);  		//constuctor 
	tree();
	~tree();

	int getage(); 
	void setage(int age);
	int getsugar() ;
	void setsugar (int sugar) ;
	int getleaves() ;
	void setleaves (int leaves) ;
	void growleave (int n) ;
	
	void dfosy (int n)	;	//dag fotosynthese 
	void nfosy (int n)	;	//nacht fotosynthese 

	void showgrowspeed (); 

	tree * createsubtree (int treespec) ; // function gets a int 1,2 or 3 to determen which tree will be created, function returns a pointer to the new object 

protected:  
	int itsage ;
	int itssugar ;
	int itsleaves ;
	int growspeed ;	//virtual
	const int maxleaves ; //constant? 
} ;

tree::tree (int age, int sugar, int leaves) : maxleaves (50)
{
	itsage = age ;
	itssugar = sugar ;
	itsleaves = leaves ;
}

tree::tree ()  : maxleaves (50)
{
	itsage = 1 ;
	itssugar = 5 ;
	itsleaves = 1 ;
}


class oak : public tree			//afleiding eik klasse 
{
public: 
	oak() ;						//afgeleide constructor 
	~oak() ;
} ;

oak::oak () 
{ 
	int growspeed = 2 ;
} ;


class maple : public tree		//afleiding esdoorn klasse 
{ 
public:
	maple () ;					//afgeleide constructor 
	~maple () ;
} ;

maple::maple ()
{
	int growspeed = 3 ;
} ;


class ash : public tree			//afleiding es klasse 
{ 
public:
	ash () ;						//afgeleide constructor 
	~ash () ;
} ;	

ash::ash() 
{
	int growspeed = 2 ;
} ;


int tree::getage()
	{
		return itsage ;
	} 

void tree::setage(int age) 
	{
		itsage = age ;
	} 

int tree::getsugar()
	{
		return itssugar ;
	} 

void tree::setsugar(int sugar) 
	{
		itssugar = sugar ;
	} 

int tree::getleaves()
	{
		return itsleaves ;
	} 

void tree::setleaves(int leaves) 
	{
		itsleaves = leaves ;
	} 

void tree::growleave (int n) 
	{
		while (n != 0) 
		{
			itssugar = itssugar -5 ;
			itsleaves++ ;
			n-- ;
		}
	}

void tree::dfosy (int n) 
	{
		while (n != 0)		//geen +n (als n >1)gebruikt, ivm toevoegen functies.  
		{ 
			itssugar = itssugar + itsleaves ; 
			n-- ; 
		}
	} 

void tree::nfosy (int n) 
	{
		while (n != 0)		//geen +n (als n >1)gebruikt, ivm toevoegen functies.  
		{ 
			itssugar = itssugar - itsleaves ;
			n-- ;
		}
	}
void tree::showgrowspeed ()
{
	cout << "The growspeed is: " << growspeed << ". " ;
} ;


tree * tree::createsubtree (int treespec) 
{ 
	if (treespec == 1) 
	{
		oak *ptreesubfuc01 = new oak ;
		return (ptreesubfuc01) ;
	}
	else if (treespec == 2)
	{
		maple *ptreesubfuc01 = new maple ;
		return (ptreesubfuc01) ;
	}
	else if (treespec == 3)
	{
		ash *ptreesubfuc01 = new ash ;
		return (ptreesubfuc01) ;
	}
	else 
	cout << "No valid input received" ;
} 



int main () 
{
	int treechoise ;
	cout << "Please enter a number to create a tree. \nPress 1 to create an Oak tree \nPress 2 to create a Maple tree \nPress 3 to create an Ash tree \n" ;
	cin >> treechoise ;
	
	tree *ptree01 = tree.createsubtree (treechoise) ;
	
	

	int menuchoise ;
	int noa ;		//number of actions 
	bool menu = true ;

	while (menu) 
	{ 
		cout << "\nSelected tree: " << treechoise << ".\n" ;
		cout << "\nplease select an event\n" << "Press 1 to see sugar level\nPress 2 to see number of leaves\nPress 3 to grow a new leaf\nPress 4 to start photosynthesis\nPress 5 to start night mode\nPress 6 to see the grow speed\nPress 7 to see the age\nPress 8 to exit.\n" ;
		
		if (menuchoise == 1)  
		{
			cout << "\nThe tree has " << ptree01->getsugar() << " sugar.\n" ;
			break ;
		} 
		else if (menuchoise == 2) 
		{
			cout << "\nThe tree has " << ptree01->getleaves() << " leaves.\n" ;
			break ;
		}
		else if (menuchoise == 3) 
		{
			cout << "\nGrowing a new leave. \n" ;
			ptree01->growleave(1) ;
			break ; 
		}
		else if (menuchoise == 4) 
		{
			cout << "\nStarting photosynthesis.  \n" << "How many times should photosynthesis take place?\n" ;
			cin >> noa ;
			ptree01->dfosy(noa) ;
			break ;
		}
		else if (menuchoise == 5)
		{
			noa = 0 ;
			cout << "\nStarting night.  \n" << "How many night should take place?\n" ;
			cin >> noa ;
			ptree01->nfosy(noa) ;
			break ; 
		}
		else if (menuchoise == 6)
		{		
			cout << "\nThe growspeed is " ;
			ptree01->showgrowspeed() ;
			cout << ".\n" ;
			break ; 
		}
		else if (menuchoise == 7)
		{
			cout << "\nThe tree is " << ptree01->getage() << " years old.\n";
			break ; 
		}
		else 
		{
			cout << "\nExiting\n" ;
			menu = false ;
			delete ptree01 ;
		}
	} 
	
return (0) ;
}




Compiling...
treetest03.cpp
path(189) : warning C4832: token '.' is illegal after UDT 'tree'
path(13) : see declaration of 'tree'
path(189) : error C2275: 'tree' : illegal use of this type as an expression
path(13) : see declaration of 'tree'
Build log was saved
treetest03 - 1 error(s), 1 warning(s)
 
	tree *ptree01 = tree.createsubtree (treechoise) ;


You need to have an instance of the tree class in order to call the createsubtree
member function.

Hi all. Thanks for the help!!
I almost got it working, infact it's working already. But not the way it should. I can compile but only if I comment out the delete pointer lines (that can't be good). (I gues something is wrong with my destructors)
And there is another problem, when I asked a created tree for it's growspeed I got a really strange number (I suspect it was what just happend to be in the memory, so my derived class didn't initialise the growspeed). I changed my code to initialise a growspeed of 0 in my main class, and I still (try to) redefine it in my subclasses. But when I ask for the growspeed I keep getting 0.



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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
#include "stdafx.h" 

#include <iostream> 

using namespace std ;


class tree 
{
public: 
	
	tree(int age, int sugar, int leaves);  		//constuctor 
	tree();
	~tree();

	int getage(); 
	void setage(int age);
	int getsugar() ;
	void setsugar (int sugar) ;
	int getleaves() ;
	void setleaves (int leaves) ;
	void growleave (int n) ;
	
	void dfosy (int n)	;	//dag fotosynthese 
	void nfosy (int n)	;	//nacht fotosynthese 

	void showgrowspeed (); 

	tree * createsubtree (int treespec) ; // function gets a int 1,2 or 3 to determen which tree will be created, function returns a pointer to the new object 

protected:  
	int	itsage ;
	int itssugar ;
	int itsleaves ;
	int itsgrowspeed ;		//virtual initialised as 0
	const int maxleaves ; 
} ;

tree::tree (int age, int sugar, int leaves) : maxleaves (50)
{
	itsage = age ;
	itssugar = sugar ;
	itsleaves = leaves ;
	itsgrowspeed = 0 ;
}

tree::tree ()  : maxleaves (50)
{
	itsage = 1 ;
	itssugar = 5 ;
	itsleaves = 1 ;
	itsgrowspeed = 0 ;
}


class oak : public tree			//afleiding eik klasse 
{
public: 
	oak() ;						//afgeleide constructor 
	~oak() ;
} ;

oak::oak () 
{ 
	int itsgrowspeed = 2 ;
} ;

class maple : public tree		//afleiding esdoorn klasse 
{ 
public:
	maple () ;					//afgeleide constructor 
	~maple () ;
} ;

maple::maple ()
{
	int itsgrowspeed = 3 ;
} ;


class ash : public tree			//afleiding es klasse 
{ 
public:
	ash () ;						//afgeleide constructor 
	~ash () ;
} ;	

ash::ash() 
{
	int itsgrowspeed = 2 ;
} ;


int tree::getage()
	{
		return itsage ;
	} 

void tree::setage(int age) 
	{
		itsage = age ;
	} 

int tree::getsugar()
	{
		return itssugar ;
	} 

void tree::setsugar(int sugar) 
	{
		itssugar = sugar ;
	} 

int tree::getleaves()
	{
		return itsleaves ;
	} 

void tree::setleaves(int leaves) 
	{
		itsleaves = leaves ;
	} 

void tree::growleave (int n) 
	{
		while (n != 0) 
		{
			itssugar = itssugar -5 ;
			itsleaves++ ;
			n-- ;
			itsage++ ;
		}
	}

void tree::dfosy (int n) 
	{
		while (n != 0)		//geen +n (als n >1)gebruikt, ivm toevoegen functies.  
		{ 
			itssugar = itssugar + itsleaves ; 
			n-- ; 
			itsage++ ;
		}
	} 

void tree::nfosy (int n) 
	{
		while (n != 0)		//geen +n (als n >1)gebruikt, ivm toevoegen functies.  
		{ 
			itssugar = itssugar - itsleaves ; 
			n-- ; 
			itsage++ ; 
		}
	}
void tree::showgrowspeed ()
{
	cout << "The growspeed is: " << itsgrowspeed << ". " ;
} ;


tree * tree::createsubtree (int treespec) 
{ 
	
	if (treespec == 1) 
	{
		oak *ptreesubfuc01 = new oak ;
		return (ptreesubfuc01) ;
				}
	else if (treespec == 2)
	{
		maple *ptreesubfuc01 = new maple ;
		return (ptreesubfuc01) ;
	} 
	else				//else allowed because treespec is checked to be 1, 2 or 3 
	{
		ash *ptreesubfuc01 = new ash ;
		return (ptreesubfuc01) ;
	}
	
} 


int main () 
{
	int treechoise ;
	bool treechoisevalid = false ;
	while (treechoisevalid == false) 
	{
		cout << "\nPlease enter a number to create a tree. \nPress 1 to create an Oak tree \nPress 2 to create a Maple tree \nPress 3 to create an Ash tree \n" ;
		cin >> treechoise ; 

		if (treechoise > 0)		//to make sure createsubtree(treechoise) doesn't get invalid input  
		{
			if (treechoise < 4)
			{
				treechoisevalid = true ;
			}
			else 
			{ 
				cout << "\nInvalid input, input to high.\n\n" ;
			}
		}
		else 
		{
			cout << "\nInvalid input, input to low.\n\n" ; 
		}
	}

	tree *pdummytree = new tree ;
	tree *ptree01 = pdummytree->createsubtree(treechoise) ;
	delete pdummytree ;

	int menuchoise ;
	int noa ;		//number of actions 
	bool menu = true ;
	cout << "\nSelected tree: " << treechoise << ".\n" ; 

	while (menu) 
	{ 
		
		cout << "\nPlease select an event:\n" << "Press 1 to see sugar level\nPress 2 to see number of leaves\nPress 3 to grow a new leaf\nPress 4 to start photosynthesis\nPress 5 to start night mode\nPress 6 to see the grow speed\nPress 7 to see the age\nPress 8 to exit.\n" ;
		cin >> menuchoise ;

		if (menuchoise == 1)  
		{
			cout << "\nThe tree has " << ptree01->getsugar() << " sugar.\n" ;
			continue ;
		} 
		else if (menuchoise == 2) 
		{
			cout << "\nThe tree has " << ptree01->getleaves() << " leaves.\n" ;
			continue ;
		}
		else if (menuchoise == 3) 
		{
			cout << "\nGrowing a new leave. \n" ;
			ptree01->growleave(1) ;
			continue ; 
		}
		else if (menuchoise == 4) 
		{
			cout << "\nStarting photosynthesis.  \n" << "How many times should photosynthesis take place?\n" ;
			cin >> noa ;
			ptree01->dfosy(noa) ;
			continue ;
		}
		else if (menuchoise == 5)
		{
			noa = 0 ;
			cout << "\nStarting night.  \n" << "How many night should take place?\n" ;
			cin >> noa ;
			ptree01->nfosy(noa) ;
			continue ; 
		}
		else if (menuchoise == 6)
		{		
			cout << "\nThe growspeed is " ;
			ptree01->showgrowspeed() ;
			cout << ".\n" ;
			continue ; 
		}
		else if (menuchoise == 7)
		{
			cout << "\nThe tree is " << ptree01->getage() << " days old.\n";
			continue ; 
		}
		else 
		{
			cout << "\nExiting\n" ;
			menu = false ;
			continue ;
			delete ptree01 ;
		}
	} 
	
return (0) ;
}





Compiling...
treetest03.cpp
Linking...
treetest03.obj : error LNK2001: unresolved external symbol "public: __thiscall tree::~tree(void)" (??1tree@@QAE@XZ)
path : fatal error LNK1120: 1 unresolved externals
Build log was saved
treetest03 - 2 error(s), 0 warning(s)
hi again


first I suggest you make the declaration of your classes and then implement
your class methods below your class declararions.It's easier to read (at least for me).

Second about your mistakes.The problem is in delete ptree01 ;.Why?
Because ptree01 is a pointer to a tree object.pointer to base object can point to
obects of derived classes but they can only use methods of the base class or of the class they are declared to be for example

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
class aaa
{
    protected:
        int x;
        int y;
   public:
        aaa();
        void doSmth();
};

class bbb : public aaa
{
   protected:
      string name;
   public:
      bbb();
      void display();
};

int main()
{
     aaa first;
     bbb second;

    aaa *paaa = new aaa;
    aaa *paaa = &second;  // valid
    paa -> display(); // invalid aaa class doesn't contain 
                               // any display() method

    // rest of the code...

}



this applies for destructors too.

for examplea statement like this

 
delete paa;


would call for aaa destructor leading to x, y be deleted but name stays untouched


the solution is to declare you destructors virtuals


not much time
let me know about your progress

Last edited on
Thanks everyone for your help!!
I reorganised my constructors, destructors en methodes.
I solved the problem, I had a virtual destructor, but I didn't place {} behind it. :S
I also solved my problem with my get growspeed, and I added some other functions. I'll post my new code so people who have the same problem can see how I solved it.
Thanks again for the help. (suggestions on how to improve the code further are very welcome)



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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
#include "stdafx.h" 
#include <iostream> 
#include <string>

using namespace std ;

//class declarations

class tree				
{
public: 
	
	tree(int age, int sugar, int leaves) ;  		//constuctor 
	tree() ;
	~tree() {} ;

	int getage() ; 
	void setage(int age) ;
	int getsugar() ;
	void setsugar (int sugar) ;
	int getleaves() ;
	void setleaves (int leaves) ;
	void growleave (int n) ;
	
	void dfosy (int n)	;		//dag fotosynthese 
	void nfosy (int n)	;		//nacht fotosynthese 

	virtual void showgrowspeed () ; 
	virtual string getitsname () ;

	tree * createsubtree (int treespec) ; // function gets a int 1,2 or 3 to determen which tree will be created, function returns a pointer to the new object 

protected:  
	int	itsage ;
	int itssugar ;
	int itsleaves ;
	int itsgrowspeed ;			//virtual initialised as 0
	const int maxleaves ;
	string itsname ;
} ;

class oak : public tree			//afleiding eik klasse 
{
public: 
	oak() ;						//afgeleide constructor 
	~oak() {} ;
} ;

class maple : public tree		//afleiding esdoorn klasse 
{ 
public:
	maple() ;					//afgeleide constructor 
	~maple() {} ;
} ;

class ash : public tree			//afleiding es klasse 
{ 
public:
	ash() ;						//afgeleide constructor 
	~ash() {} ;
} ;

//constructors 

tree::tree (int age, int sugar, int leaves) : maxleaves (50)
{
	itsage = age ;
	itssugar = sugar ;
	itsleaves = leaves ;
	itsgrowspeed = 0 ;
}

tree::tree ()  : maxleaves (50)
{
	itsage = 1 ;
	itssugar = 5 ;
	itsleaves = 1 ;
	itsgrowspeed = 0 ;
	itsname = "Tree" ;
}

oak::oak () 
{ 
	itsgrowspeed = 2 ;
	itsname = "Oak" ;
} ;

maple::maple ()
{
	itsgrowspeed = 3 ;
	itsname = "Maple" ;
} ;

ash::ash() 
{
	itsgrowspeed = 2 ;
	itsname = "Ash" ;
} ;


//methode declarations

int tree::getage()
	{
		return itsage ;
	} 

void tree::setage(int age) 
	{
		itsage = age ;
	} 

int tree::getsugar()
	{
		return itssugar ;
	} 

void tree::setsugar(int sugar) 
	{
		itssugar = sugar ;
	} 

int tree::getleaves()
	{
		return itsleaves ;
	} 

void tree::setleaves(int leaves) 
	{
		itsleaves = leaves ;
	} 

void tree::growleave (int n) 
	{
		while (n != 0) 
		{
			itssugar = itssugar -5 ;
			itsleaves++ ;
			n-- ;
			itsage++ ;
		}
	}

void tree::dfosy (int n) 
	{
		while (n != 0)			//geen sugar +n (als n >1)gebruikt, ivm toevoegen functies.  
		{ 
			itssugar = itssugar + itsleaves ; 
			n-- ; 
			itsage++ ;
		}
	} 

void tree::nfosy (int n) 
	{
		if ( (itssugar - (n*itsleaves)) > 0 )
		{		
			while (n != 0)		//geen +n (als n >1)gebruikt, ivm toevoegen functies.  
			{ 
				itssugar = itssugar - itsleaves ; 
				n-- ; 
				itsage++ ; 
			}
		}
		else 
		{ 
			cout << "\nNot enouch sugar available\n" ; 
		}
	}

void tree::showgrowspeed ()
{
	cout << "The growspeed is: " << itsgrowspeed << ". " ;
} ;

string tree::getitsname () 
{
	return itsname ;
}


tree * tree::createsubtree (int treespec) 
{ 
	
	if (treespec == 1) 
	{
		oak *ptreesubfuc01 = new oak ;
		return (ptreesubfuc01) ;
	}
	else if (treespec == 2)
	{
		maple *ptreesubfuc01 = new maple ;
		return (ptreesubfuc01) ;
	} 
	else					//else allowed because treespec is checked to be 1, 2 or 3  
	{
		ash *ptreesubfuc01 = new ash ;
		return (ptreesubfuc01) ;
	}
	
} 


int main () 
{	
	tree *pdummytree = new tree ;
	tree *ptree01 = new tree ;
	int treechoise ;					//choise which tree must be created 
	int treeselected = 1 ;
	tree *arrayoftreepointers[3] ;		//create array for 3 trees

	for (int i = 0; i < 3; i++)
	{	
		cout << "Please enter a number to create a tree. \nPress 1 to create an Oak tree \nPress 2 to create a Maple tree \nPress 3 to create an Ash tree \n\n" ;
		cin >> treechoise ; 

		if (!(treechoise > 0 && treechoise < 4))	//to make sure createsubtree(treechoise) doesn't get invalid input  
		{
			i-- ;
			cout << "\nInvalid input, input to low.\n\n" ; 	
			continue ;
		}

	tree *ptree01 = pdummytree->createsubtree(treechoise) ;
	arrayoftreepointers[i] = ptree01 ;
	}
	
	int menuchoise ;
	int noa ;			//number of actions 
	bool menu = true ;
	

	do 
	{
	cout << "\nPlease select a tree (1, 2 or 3)\n" ;
	cin >> treeselected ;
	ptree01 = arrayoftreepointers[treeselected -1] ; 
	}
	while (!(treeselected >0 && treeselected <4)) ;
	
	while (menu) 
	{ 
		cout << "\nSelected tree: " << treeselected <<", " << ptree01->getitsname() << ".\n" ;
		cout << "\nPlease select an event:\n" << "Press 1 to see sugar level\nPress 2 to see number of leaves\nPress 3 to grow a new leaf\nPress 4 to start photosynthesis\nPress 5 to start night mode\nPress 6 to see the grow speed\nPress 7 to see the age\nPress 8 to select another tree\nPress 9 to exit.\n" ;
		cin >> menuchoise ;
		switch (menuchoise) 
		{
			case 1:  
			{
				cout << "\nThe tree has " << ptree01->getsugar() << " sugar.\n" ;
				continue ;
			} 
			
			case 2: 
			{
				cout << "\nThe tree has " << ptree01->getleaves() << " leaves.\n" ;
				continue ;
			}
			
			case 3: 
			{
				cout << "\nGrowing a new leave. \n" ;
				ptree01->growleave(1) ;
				continue ; 
			}
			
			case 4: 
			{
				cout << "\nStarting photosynthesis.  \n" << "How many times should photosynthesis take place?\n" ;
				cin >> noa ;
				ptree01->dfosy(noa) ;
				continue ;
			}
			
			case 5: 
			{
				noa = 0 ;
				cout << "\nStarting night.  \n" << "How many night should take place?\n" ;
				cin >> noa ;
				ptree01->nfosy(noa) ;
				continue ; 
			}
			
			case 6: 
			{		
				cout << "\nThe growspeed is " ;
				ptree01->showgrowspeed() ;
				cout << ".\n" ;
				continue ; 
			}
			
			case 7: 
			{
				cout << "\nThe tree is " << ptree01->getage() << " days old.\n";
				continue ; 
			}
			
			case 8: 
			{
				do 
				{ 
					cout << "\nPlease select a tree\n" ;
					cin >> treeselected ;
					if (treeselected >0 && treeselected <4) 
					{
						ptree01 = arrayoftreepointers[treeselected -1] ; 
					}
				}
				while (!(treeselected >0 && treeselected <4)) ;
				continue ;
			} 	
			
			default: 
			{
				cout << "\nExiting\n" ;
				menu = false ;
				delete ptree01 ;
				delete pdummytree ;
				continue ;
			}
		}
	} 
	
return (0) ;
}

Topic archived. No new replies allowed.