Iam new to C++ and below mentioned is the summary of the problem. Bar's constructor needs to explicitly call foo's constructor and the argument to foo's constructor has to be an object to baz, which has a default constructor. Iam not allowed to use new operator(dynamic allocation) to achieve this. I tried the below code, but C++ compiler gives me compilation errors (listed below). Can somebody please explain me what's going wrong in this code? Any help is really appreciated.
//Constructor.cpp
#include <iostream>
using namespace std; // two of two, yay!
class baz {
public:
baz() { };
};
class Foo {
public:
Foo(baz y) { }
};
class Bar {
public:
Foo x;
baz y;
Bar() : Foo(y) { };
};
int main() {
Bar b;
}
Syntax Error on compiling.
--------------------------
constructor.cpp: In constructor `Bar::Bar()':
constructor.cpp:19: error: type `Foo' is not a direct base of `Bar'
constructor.cpp:19: error: no matching function for call to `Foo::Foo()'
constructor.cpp:9: note: candidates are: Foo::Foo(const Foo&)
constructor.cpp:11: note: Foo::Foo(baz)