Reputation: 39
Why in the underlying example keeps the compiler complaining about a redefinition for the B constructor. If i remove the {} it complains they need to be there. How is this done correctly? I want to implement the constructor of B in the CPP file and not inline.
#include <vector>
#include <sstream>
#include <iostream>
using namespace std;
class A
{
public:
A();
~A();
};
class B : public A
{
public:
B() : A() {};
~B();
};
A::A()
{
cout << "A construct!";
}
B::B()
{
cout << "B construct!";
cout << "B construct!";
cout << "B construct!";
cout << "B construct!";
cout << "B construct!";
cout << "B construct!";
cout << "B construct!";
cout << "B construct!";
cout << "B construct!";
cout << "B construct!";
}
int main()
{
return 0;
}
Upvotes: 0
Views: 2110
Reputation: 69027
You have already defined B constructor in the class definition:
class B : public A
{
public:
B() : A() {};
~B();
};
So, when you define it again outside of the class definition:
B::B()
{
cout << "B construct!";
you get the error; if the correct constructor is the last one, replace your class definition with this one:
class B : public A
{
public:
B();
~B();
};
If you want to specify the base class constructor in B constructor definition, you can do:
B::B()
: A()
{
cout << "B construct!";
Upvotes: 4
Reputation: 20272
Remove : A() {}
, that's the body of your constructor, not just {}
. Add the initializer list to the body B::B()
you have later on.
Upvotes: 1