Undeclared identifier

Hi, I'm quite new to c++ and i've gotten a bit stuck with my program.
It's still in the middle of being built but so far its supposed to recieve an input pass it to the hexagon class then the hexagon class is supposed to use it to print out the information.

but it keeps popping up with the error undeclared identifier plus a couple others.

code below:
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
#include <iostream>

using namespace std;

int main()
{
	int sizeInput, typeInput,option;
	cout << "Would you like to draw a hexagon? \n";
	cout << "1: Yes \n";
	cout << "2: No \n";
	cin >> option;
	while(option == 1)
	{
	cout << "Please enter size of hexagon \n";
	cin >> sizeInput;
	cout << "Please choose type of hexagon \n";
	cout << "1: Type 1 \n";
	cout << "2: Type 2 \n";
	cout << "3: Type 3 \n";
	cout << "4: Type 4 \n";
	cin >> typeInput;
	Hexagon h1(sizeInput, typeInput);
	//cout << sizeInput << typeInput << endl;
	
	cout << "Would you like to draw a hexagon? \n";
	cout << "1: Yes \n";
	cout << "2: No \n";
	cin >> option;
	}
	cin.ignore(2);
}


class Hexagon
{
private:
	int size;
	int type;
public:
	//int getSize() {return size;}
	//int getType() {return type;}
	Hexagon(int sizeValue = 0, int typeValue = 0) :	size(sizeValue), type(typeValue)
	{
	}
	
	~Hexagon()
	{
	}
	void calculation()
	{
		if(type ==1)
		{
			for (int rows=0; rows<size+1; rows++)
			{
				for (int spaces=0; spaces < (size - rows / 2); spaces++)
				{
					cout << " ";
				}

				for (int stars=0; stars<rows+1; stars++)
				{	
					cout << "*";
				}
				rows++;
				cout << endl;
			}
	}
	}
	void setSize(int sizeInput) {size = sizeInput; return;}
	void setType(int typeInput) {type = typeInput; return;}
};

error message below
1>------ Build started: Project: Assignment 1, Configuration: Debug Win32 ------
1> Assignment 1.cpp
1>c:\users\hobo alan\documents\visual studio 2010\projects\assignment 1\assignment 1\assignment 1.cpp(22): error C2065: 'Hexagon' : undeclared identifier

1>c:\users\hobo alan\documents\visual studio 2010\projects\assignment 1\assignment 1\assignment 1.cpp(22): error C2146: syntax error : missing ';' before identifier 'h1'

1>c:\users\hobo alan\documents\visual studio 2010\projects\assignment 1\assignment 1\assignment 1.cpp(22): error C3861: 'h1': identifier not found

========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========


Help would be greatly appreciated.
Last edited on
The compiler needs to know about your Hexagon class before you try to use it.
Please put your code in code tags ('<>'). As for your problem, you need to declare the Hexagon class and your functions before main.
ahh thanks alot.

i stuck my class above my main and it seemed to work, i doubt thats the proper way to do it though.

is a there a better way?

because im going to be splitting the program into different class files when its done.
Putting the whole class above main is fine, but I think you can also just put class Hexagon; before main, then declare it afterwards like you did. Or, you can put it in another file ("Hexagon.h", for example), put that file in the same folder as your program, and put #include "Hexagon.h" with the rest of your include statements.
okay thanks i'll do that in the future to try and develop good habits :D.

oh and sorry about the code tags i'll make sure to do that from now on aswell.
you made hexagon with a capital H in the class?
Are you referring to this part:
1
2
3
Hexagon(int sizeValue = 0, int typeValue = 0) :	size(sizeValue), type(typeValue)
	{
	}

?

This is a constructor for the class, not an instance of it. What are you talking about?
Topic archived. No new replies allowed.