Undeclared identifier

my .cpp detect there are undefined identifier.. is there anything wrong on my header file?

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
  /*testing2.h*/
//class and method implement
class Nut
{
private:
	char type; //type of nut
	public:
		 char GetType (void)
		{
		return type;
		} 
		void SetType (char value)
	{
		type=value;
	}

};
class almond: public Nut
{
private:
	char type;
	
	public: 
	 char GetType (void)
	{
	return type;
	
	}
	void SetType (char value)
	{
	type=value;
	
	}
};
class walnut: public Nut
{ 
private :
	char type;
	public : 
	 char GetType (void)
	{ 
	return type;
	
	}
	void SetType (char value)
	{
	type= value;
	}
};
class almond_butter : public almond
{
	private:
		int carbohydrates;
		int cholestrol;
		int protein;
		int calories;
	public:
		int SetCarohydrates (int value)
		{
		carbohydrates = value;
 		}
 		int Setcholestrol (int value)
 		{ 
 		cholestrol=value;
 		}
 		int Setprotein (int value)
 		{
 			protein=value;
 		}
 		int SetCalories (int value)
 		{
 			calories=value;
 		}		
		
};
class almond_milk :public almond
{
	private:
		int carbohydrates;
		int cholestrol;
		int protein;
		int calories;
	public:
		int SetCarohydrates (int value)
		{
		carbohydrates = value;
 		}
 		int Setcholestrol (int value)
 		{ 
 		cholestrol=value;
 		}
 		int Setprotein (int value)
 		{
 			protein=value;
 		}
 		int SetCalories (int value)
 		{
 			calories=value;
 		}		
		
};
class black_walnut: public walnut
{
	private:
		int carbohydrates;
		int cholestrol;
		int protein;
		int calories;
	public:
		int SetCarohydrates (int value)
		{
		carbohydrates = value;
 		}
 		int Setcholestrol (int value)
 		{ 
 		cholestrol=value;
 		}
 		int Setprotein (int value)
 		{
 			protein=value;
 		}
 		int SetCalories (int value)
 		{
 			calories=value;
 		}		
		
};
class english_walnut: public walnut
{
	private:
		int carbohydrates;
		int cholestrol;
		int protein;
		int calories;
	public:
		int SetCarohydrates (int value)
		{
		carbohydrates = value;
 		}
 		int Setcholestrol (int value)
 		{ 
 		cholestrol=value;
 		}
 		int Setprotein (int value)
 		{
 			protein=value;
 		}
 		int SetCalories (int value)
 		{
 			calories=value;
 		}		
		
};


and here my testing.cpp

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
#include<iostream>
#include<string>
#include "testing2.h"

using namespace std;

int main()
{
	Almond anut;
	Walnut wnut;
	Almond_butter albut;
	Almond_milk almilk;
	Black_walnut blwal;
	English_walnut enwal;
	
	anut.SetType('Almond');
	wnut.SetType('Walnut');
	
	anut.SetType('Almond_Butter');
	anut.SetType('Almond_Milk');
	wnut.SetType('Black_Walnut');
	wnut.SetType('English_Walnut');
	
	albut.SetCarbohydrates(28);
	almilk.SetCarbohydrates(20);
	blwal.SetCarbohydrates(68);
	enwal.SetCarbohydrates(44);
	
	albut.SetCholesterol(0);
	almilk.SetCholesterol(0);
	blwal.SetCholesterol(0);
	enwal.SetCholesterol(0);
	
	albut.SetProtein(30);
	almilk.SetProtein(20);
	blwal.SetProtein(29);
	enwal.SetProtein(30);
	
	albut.SetCalories(60);
	almilk.SetCalories(52);
	blwal.SetCalories(48);
	enwal.SetCalories(58);
	
cout<<""<<anut.getType()<<"is the type of nut"<<endl;
cout<<""<<wnut.getType()<<"is the type of nut"<<endl;


cout<<"The carbohydrates of Almond butter is"<<albut.GetCarbohydrates()<<"gram"<<endl;
cout<<"The carbohydrates of Almond milk is"<<almilk.GetCarbohydrates()<<"gram"<<endl;
cout<<"The carbohydrates of Black walnut is"<<blwal.GetCarbohydrates()<<"gram"<<endl;
cout<<"The carbohydrates of English walnut is"<<enwal.GetCarbohydrates()<<"gram"<<endl;

cout<<"Almond butter has"<<albut.GetCholesterol()<<"gram cholesterol"<<endl;
cout<<"Almond Milk has"<<almilk.GetCholesterol()<<"gram cholesterol"<<endl;
cout<<"Black walnut has"<<blwal.GetCholesterol()<<"gram cholesterol"<<endl;
cout<<"English walnut has"<<enwal.GetCholesterol()<<"gram cholesterol"<<endl;

cout<<"Almond butter has"<<albut.GetProtein()<<"gram protein"<<endl;
cout<<"Almond milk has"<<almilk.GetProtein()<<"gram protein"<<endl;
cout<<"Black Walnut has"<<blwal.GetProtein()<<"gram protein"<<endl;
cout<<"English Walnut has"<<enwal.GetProtein()<<"gram protein"<<endl;

cout<<"Almond butter has"<<albut.GetCalories()<<"kcal calories"<<endl;
cout<<"Almond milk has"<<almilk.GetCalories()<<"kcal calories"<<endl;
cout<<"Black Walnut has"<<blwal.GetCalories()<<"kcal calories"<<endl;
cout<<"English Walnut has"<<enwal.GetCalories()<<"kcal calories"<<endl;

}
1) It would be better if you posted actual error messages.

2) Compare:
1
2
3
4
5
6
class almond: public Nut
Almond anut;

class walnut: public Nut
Walnut wnut;
//etc. 
Last edited on
here the errors.. too much error about undeclared identifier..

testing.cpp(9): error C2065: 'Almond' : undeclared identifier
testing.cpp(9): error C2146: syntax error : missing ';' before identifier 'anut'
testing.cpp(9): error C2065: 'anut' : undeclared identifier
testing.cpp(10): error C2065: 'Walnut' : undeclared identifier
testing.cpp(10): error C2146: syntax error : missing ';' before identifier 'wnut'
testing.cpp(10): error C2065: 'wnut' : undeclared identifier
testing.cpp(11): error C2065: 'Almond_butter' : undeclared identifier
testing.cpp(11): error C2146: syntax error : missing ';' before identifier 'albut'
testing.cpp(11): error C2065: 'albut' : undeclared identifier
testing.cpp(12): error C2065: 'Almond_milk' : undeclared identifier
testing.cpp(12): error C2146: syntax error : missing ';' before identifier 'almilk'
testing.cpp(12): error C2065: 'almilk' : undeclared identifier
testing.cpp(13): error C2065: 'Black_walnut' : undeclared identifier
'Almond' : undeclared identifier
Well, you do not have class Almond anywhere, so compiler gives you an error.
Look at point 2 in my previous post again.
Furthermore: You haven't defined functions like GetCarbohydrates() and the other getters.

I think you misunderstand inheritance. class almond: public Nut implies that everything part of Nut is also part of almond. In other words: type exists twice in almond (type int Nut is just not accessible in case of the variable type almond)
oh i see.. now is error about too many characters in constant

1
2
3
4
5
6
7
testing.cpp(16): error C2015: too many characters in constant
testing.cpp(17): error C2015: too many characters in constant
testing.cpp(19): error C2015: too many characters in constant
testing.cpp(20): error C2015: too many characters in constant
testing.cpp(21): error C2015: too many characters in constant
testing.cpp(22): error C2015: too many characters in constant
Because you set your SetType() function to accept a char. And you trying to pass a string (and actually passing an int: multicharacter literal)
ok settle the things up there and now

1
2
3
testing.cpp(24): error C2039: 'SetCarbohydrates' : is not a member of 'almond_butter'
testing.cpp(25): error C2039: 'SetCarbohydrates' : is not a member of 'almond_milk'
testing.cpp(26): error C2039: 'SetCarbohydrates' : is not a member of 'black_walnut'
coder777 wrote:
Furthermore: You haven't defined functions like GetCarbohydrates() and the other getters.
Topic archived. No new replies allowed.