order of class declarations

I was trying to make an example where the declarations of two classes are interlinked and I came up with the following example.
Please, let me know what is a proper way of making the code work.

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
#include <iostream>

using namespace std;

// Class Chicken:
class Chicken{
public:
   Chicken();
   Egg * makeEgg();
};

// Class Egg:
class Egg{
public:
   Egg();
   ~Egg();
   Chicken * makeChicken();
};

public Egg * Chicken::makeEgg(){
   return new Egg();
};

public Chicken * Egg::makeChicken(){
   this->~Egg();
   return new Chicken;
}


int main(){
   cout << "What was first, the chicken or the egg?" << endl;
   cout << "Enter 1 for the chicken and 2 for the egg:" << endl;

   int creationChoice;
   cin >> creationChoice;

   Chicken * littleChicken;
   Egg * roundEgg;

   if (creationChoice==1){
      littleChicken = new Chicken();
      roundEgg = littleChicken->makeEgg();
   }else{
      roundEgg = new Egg();
      littleChicken = roundEgg->makeChicken();
   }

   cout << "Congratulations! The evolutionary question has been resolved.\n";

   return 0;
}



In its current form I am getting the following errors:

line 8: error: 'Egg' does not name a type
I assume this is because the class Egg was not yet defined.

line 18: error: expected unqualified-id before 'public'
line 22: error: expected unqualified-id before 'public'
These two are a mystery to me.

line 41: error: 'class Chicken' has no member named 'makeEgg'
It seams that I should only pay attention to the first error, as all later errors can be its consequence.

line 8: error: 'Egg' does not name a type
I assume this is because the class Egg was not yet defined.

That's it. All you have in the Chicken class definition is a pointer to Egg so you don't need the full class definition of Egg. A forward declaration is enough.
class Egg;

line 18: error: expected unqualified-id before 'public'
line 22: error: expected unqualified-id before 'public'
These two are a mystery to me.

You should not put public at the beginning of the line when defining the member functions outside the class definition.
Last edited on
Thank you Peter. This resolved the main issues.
I had couple more small things (like missing definitions of constructors and destructors), but it works now.
Thanks a lot!
closed account (NyhkoG1T)
From what I can see, the "missing definitions of constructors and destructors" is because you prototype the construct of both chicken and egg inside the class, "chicken(); and egg();" respectively, but then you don't define what to initialize. Also, you prototype the Egg destructor with "~Egg();" without following up with a defined destructor. You can either define them, or remove the prototypes, either way you can still directly refer to them because if you dont prototype or define a constructor/destructor, then it is done for you.

So, if you remove "Egg();" and "~Egg();" from the prototype inside Egg class, you will STILL be able to do this

1
2
3
4
public Chicken * Egg::makeChicken(){
   this->~Egg();
   return new Chicken;
}
Topic archived. No new replies allowed.