I am using C++ to make a basic family with polymorphism. However, I get errors when I compile, and I am not sure how to correct them (I say class dependency
, as Parent and Child both depend on each other before the other is created):
error C2061: syntax error : identifier 'Parent'
error C2143: syntax error : missing ';' before '*'
error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
error C2065: 'parent' : undeclared identifier
error C2065: 'name' : undeclared identifier
error C2614: 'Child' : illegal member initialization: 'par' is not a base or member
error C2065: 'par' : undeclared identifier
error C2227: left of '->addChild' must point to class/struct/union/generic type
error C2065: 'par' : undeclared identifier
error C2227: left of '->Name' must point to class/struct/union/generic type
error C2661: 'Child::Child' : no overloaded function takes 2 arguments
error C2065: 'par' : undeclared identifier
error C2227: left of '->Name' must point to class/struct/union/generic type
/* Polymorphism Family */
#include <iostream>
#include <vector>
class Child{
public:
Child(Parent* parent, char* name) : par(parent), Name(name){ par->addChild(this); };
Parent* par;
char* Name;
virtual void Speak(void){
std::cout << "I am a child, my name is " << Name << " and my parent's name is " << par->Name << std::endl;
}
};
class Parent{
public:
Parent(char* name) : Name(name){};
char* Name;
std::vector<Child*> Children;
virtual void Speak(void){
std::cout << "I am a parent called " << Name << std::endl;
if(Children.size() == 0){
std::cout << "I have no children" << std::endl;
}
else{
std::cout << "I have " << Children.size() << " children:" << std::endl;
for(auto c : Children){
std::cout << c->Name;
}
}
}
void addChild(Child* child){
Children.push_back(child);
}
};
class Father : public Parent{
public:
Father(char* name) : Parent(name){}
void Speak(void){
std::cout << "I am a father called " << Name << std::endl;
if(Children.size() == 0){
std::cout << "I have no children" << std::endl;
}
else{
std::cout << "I have " << Children.size() << " children:" << std::endl;
for(Child* c : Children){
std::cout << c->Name;
}
}
}
};
class Son : public Child{
public:
Son(Parent* parent, char* name) : Child(parent, name){}
void Speak(void){
std::cout << "I am a son, my name is " << Name << " and my parent's name is " << par->Name << std::endl;
}
};
int main(void){
Father* Dad = new Father("James");
Dad->Speak();
Son* John = new Son(Dad, "John");
John->Speak();
Dad->Speak();
delete[] Dad, John;
getchar();
return (0);
}
Edit: I have 2 errors now:
/* Polymorphism Family */
#include <iostream>
#include <vector>
class Parent;
class Child{
public:
Child(Parent* parent, char* name);
Parent* par;
char* Name;
virtual void Speak(void){
std::cout << "I am a child, my name is " << Name << " and my parent's name is " << par->Name << std::endl;
}
};
class Parent{
public:
Parent(char* name) : Name(name){};
char* Name;
std::vector<Child*> Children;
virtual void Speak(void){
std::cout << "I am a parent called " << Name << std::endl;
if(Children.size() == 0){
std::cout << "I have no children" << std::endl;
}
else{
std::cout << "I have " << Children.size() << " child(ren):" << std::endl;
for(auto c : Children){
std::cout << c->Name << std::endl;
}
}
}
void addChild(Child* child){
Children.push_back(child);
}
};
Child::Child(Parent* parent, char* name){
Child::par = parent;
Child::Name = name;
par->addChild(this);
};
class Father : public Parent{
public:
Father(char* name) : Parent(name){}
void Speak(void){
std::cout << "I am a father called " << Name << std::endl;
if(Children.size() == 0){
std::cout << "I have no children" << std::endl;
}
else{
std::cout << "I have " << Children.size() << "child(ren):" << std::endl;
for(auto c : Children){
std::cout << c->Name << std::endl;
}
}
}
};
class Son : public Child{
public:
Son(Parent* parent, char* name) : Child(parent, name){}
void Speak(void){
std::cout << "I am a son, my name is " << Name << " and my parent's name is " << par->Name << std::endl;
}
};
int main(void){
Father* Dad = new Father("James");
Dad->Speak();
Son* John = new Son(Dad, "John");
John->Speak();
Dad->Speak();
delete[] Dad, John;
getchar();
return (0);
}