Dynamic Memory Allocation Problem - "expected type-specifier before"

Hey Guys,

So I am new here, and am totally new to programming. I am trying to take a c++ programming course, and to be completely honest, am lost half the time. Could anyone help me with my code? I am trying to do the following:

"Write a program in which you create a Hen class. Inside this class, nest a Nest class. Inside Nest, place an Egg class. Each class should have a display() member function. For each class, create a constructor and a destructor that prints an appropriate message when it is called. In main(), create an instance of each class using new and call the display() function for each one. After calling display(), free the storage using delete."

My problem: I keep getting the error message: "expected type-specifier before ‘Egg’ Hen::Nest::Egg *e = new Egg();"

I apologize if this is a trivial question, but any suggestions to fix the problem, correct my sloppy coding, etc. would be greatly appreciated!

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
#include <iostream> // Stream Declarations
using namespace std;

class Hen {
public:
	Hen() { // Constructor
		cout << "Hen created" << endl;
	}
	~Hen() { // Destructor
		cout << "Hen destroyed" << endl;
	}
	void display() {
		cout << "Hen::display" << endl;
	}
	
	class Nest {
	public:
		Nest() { // Constructor
			cout << "Nest created" << endl;
		}
		~Nest()  { // Destructor
			cout << "Nest destroyed" << endl;
		}
		void display() {
			cout << "Hen::Nest::display" << endl;
		}
	
		class Egg {
			public:
				Egg() { // Constructor
					cout << "Egg created" << endl;
				}
				~Egg() { // Destructor
					cout << "Egg destroyed" << endl;
				}
				void display() {
					cout << "Hen::Nest::Egg::display" << endl;
				}
		};
	};
};

int main() {
	Hen *h = new Hen();
	Hen::Nest *n = new Nest();
	Hen::Nest::Egg *e = new Egg();

	h -> display();
	n -> display();
	e -> display();
		
	delete h;
	delete n;
	delete e;
} ///:~ 
You have to write new Hen::Nest; and new Hen::Nest::Egg; or else the compiler has no idea what type you are talking about.
Last edited on
Oh man... That's embarassing. Thanks LB! That makes much more sense and solved the problem! Appreciate it!
Last edited on
Topic archived. No new replies allowed.