-1

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?

Amaterastis
  • 477
  • 3
  • 12
  • 2
    MainClass::MainClass (int a, int b, std::vector& c) : Sub1(a), Sub2(b), Sub3(c) {} – OznOg May 23 '19 at 16:55
  • https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list – Jesper Juhl May 23 '19 at 16:56
  • It's not shown, but I bet you have circular dependencies. This could be the cause of your problem. [This question](https://stackoverflow.com/questions/625799/resolve-build-errors-due-to-circular-dependency-amongst-classes) may be relevant to you. – François Andrieux May 23 '19 at 16:58
  • 3
    "This produces an error". You need to post a [mcve], which includes a complete source file everyone can try to compile, *and* the text of error messages. – n. m. could be an AI May 23 '19 at 17:10
  • Well, as these are scattered across different .cpp and .h files, I think this is the minimal possible solution. The error is so vast that I cannot even copy-paste it. I'll edit and say what the error is about. – Amaterastis May 23 '19 at 17:20
  • @Amaterastis: The problem is you are not saying the correct things and we would have an all day conversation about what part of the error is valid and not valid. Unless we can reproduce the problem how do you expect us to help (guess?). That helps nobody. You simply need to post the code in a way we can reproduce the error (which means what is in source files what is in header files (the header file names) and all the includes. Which also means we need the error (so we can see we are reproducing the same error as you). – Martin York May 23 '19 at 18:30
  • Works for me: https://onlinegdb.com/ByohoPEpE – Martin York May 23 '19 at 18:52

1 Answers1

1

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.

Dmitry Kuzminov
  • 6,180
  • 6
  • 18
  • 40
  • That was my first attempt. It just produces too much errors for me to copy them. – Amaterastis May 23 '19 at 16:58
  • Generally, the errors implies: Constructor Sub1::Sub1(), Sub2::Sub2(), Sub3::Sub3() cannot be found. It ignores the fact that there is another constructor that I am trying to call. – Amaterastis May 23 '19 at 16:59
  • Make forward declarations of all classes. That solves the issue with "cannot be found" but in this case you need to rework the way you store MainClass inside of the SubX classes. Try to store it as references, pointers or smart pointers. – Dmitry Kuzminov May 23 '19 at 17:02
  • @Amaterastis I've found that I thought MainClass and MasterClass to be the same class... It is very hard to find any logic in your design without an explanation of the rationale behind that. – Dmitry Kuzminov May 23 '19 at 23:45