I am trying to create a constructor in a subclass which will call superclass constructors. Here is the code.
I have declared three classes like this:
class Sub1 {
private:
std::vector<MainClass> subvar_1;
public:
Sub1(int);
};
Sub1::Sub1(int size) : subvar_1(size) { }
_____________________________________
class Sub2 {
private:
std::vector<MainClass> subvar_2;
public:
Sub2(int);
};
Sub2::Sub2(int size) : subvar_2(size) { }
_____________________________________
class Sub3 {
private:
std::vector<std::vector<MainClass>> subvar_3;
public:
Sub3(std::vector<int>&);
};
Sub3::Sub3(std::vector<int>& size) {
subvar_3.reserve(size.size());
for (const int clmns : size)
subvar_3.emplace_back(std::vector<MainClass>(clmns));
}
Where the MainClass is defined using the following:
class MainClass {
private:
int row;
int cln;
public:
MainClass();
};
MainClass::MainClass() {
row = rand();
cln = rand();
}
After all of these have been declared, I need to put it all in a master class which is ironically the opposite:
class MasterClass : public Sub1, Sub2, Sub3 {
public:
MasterClass(int, int, std::vector<int>&);
};
This produces an error. The error talks about: not being able to find Sub1(), Sub2() and Sub3() constructors. Of course there aren't any, they are not intended to be. This error is produced when I run:
MasterClass::MasterClass(int a, int b, std::vector<int>& c) :
Sub1(a), Sub2(b), Sub3(c) { }
command.
I am compiling the code using:
g++ MainClass.cpp Sub1.cpp Sub2.cpp Sub3.cpp MasterClass.cpp
command.
Can you help me why? How can I get around this error?
What I would like to do is:
MainClass::MainClass (int a, int b, std::vector<int>& c) {
call constructor Sub1(a);
call constructor Sub2(b);
call constructor Sub3(c);
}
I trust in MasterClass that I will need:
private:
Sub1 sub1;
Sub2 sub2;
Sub3 sub3;
where I will assign the result of construction of objects that are extending the class where I am trying to call the constructor?
Is it even possible in C++ to do this (to use a class constructor which will call superclass constructor)?
If not, is there any way to get around the problem but to keep the same structure?
Try this:
MainClass::MainClass (int a, int b, std::vector<int>& c)
: Sub1(a), Sub2(b), Sub3(c) {
}
But before that clarify the problem. You have the MainClass
aggregated inside of the SubX classes. How do you plan to aggregate the class into itself? Did you mean you need references to this class from the base classes?
One possible solution is:
class Sub1;
class Sub2;
class Sub3;
class MainClass;
class Sub1 {
private:
std::vector<std::shared_ptr<MainClass>> subvar_1;
public:
Sub1(int);
};
// ...
The problem is that std::vector
requires a complete type for it's instantiation. You may wrap it into something that doesn't require that however, for example, the shared_ptr, but you need to solve this problem of the chicken or the egg:
class Sub1;
class Sub2;
class Sub3;
class MainClass;
class Sub1 {
private:
std::shared_ptr<std::vector<MainClass>> subvar_1;
public:
Sub1(int);
};
// ...
P.S. Initially I tried to wrap it into std::unique_ptr, but even that didn't work for the same reason.