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.