I'm writing some C++ classes where one class has an instance of another class as an attribute. When writing the class constructors I keep getting the error "no default constructor exists for class Foo". Here's a small example reproducing the error:
class Foo {
int size;
char name;
Foo(int s,char n) {
size = s;
name = n;
}
};
class Bar {
int size;
char name;
Foo foo;
Bar(int s, char n,Foo f){
size = s;
name = n;
foo = f;
}
};
The error disappears if I remove the class constructor for Foo so that the default constructor is used. Since I'm passing an existing instance of the class Foo into the constructor for Bar, I don't understand why the error talks about the constructor for Foo. Why does the error occur? And how can the code be fixed?