0

I am trying to have multiple instances of the same class in a parent class. Each with their own constructor arguments.

I have tried this, but not sure how to fix this. Any help would be greatly appreciated.

Base.h

class Base
{
    public:
    Base(int a, int b);
};

Parent.h

#include "Base.h"

class Base_A :Base{};
class Base_B :Base{};

class Parent : Base_A, Base_B 
{
    public:
    Parent (int a1, int a2, int b1, int b2);
}

Parent.cpp

Parent::Parent (int a1, int a2, int b1, int b2) :Base_A(a1,a2), Base_B(b1,b2)
{
    //....
}

When compiling I get the following error:

no instance of constructor "Base_A::Base_A" matches the argument list -- argument types are: (int, int)
no instance of constructor "Base_B::Base_B" matches the argument list -- argument types are: (int, int)
Misha
  • 556
  • 1
  • 8
  • 25
  • Is `base(int a, int b);` a typo? – 김선달 Feb 22 '21 at 04:32
  • _"not sure how to fix this"_ tells a story but does not describe a problem. What exactly is happening? As the above comment notes, your `Base` class does not have a constructor at all. Furthermore, `Parent` constructor is private. – paddy Feb 22 '21 at 04:34
  • Apologies, yes, 'base' was a typo and I did not add the public line for Parent constructor. I simplified original code I have. – Misha Feb 22 '21 at 04:37
  • What is the actual problem you are trying to fix? – paddy Feb 22 '21 at 04:37
  • Neither does your `Base_A`, nor `Base_B` classes have an explicit constructor. This has nothing to do, whatsoever, with "Multiple class instances". Temporarily get rid of one of them, and focus on getting everything constructed. Then, once you've figured out how to do this with `Parent` inheriting only from `Base_a`, then everything should become crystal clear. – Sam Varshavchik Feb 22 '21 at 04:38
  • I can get a single class inheritance to work perfectly by simply using class "Parent : Base {...}". But when I try "Parent : Base, Base {...}" I get the expected "class duplication error" – Misha Feb 22 '21 at 04:42
  • Seems like there is fundamental confusion over what inheritance is used for. What possible functionality are you attempting to achieve by trying to inherit the same class twice? Perhaps you should read about [Inheritance vs. Aggregation](https://stackoverflow.com/q/269496/1553090) – paddy Feb 22 '21 at 04:49
  • In my case the library that I am trying to use, has a number of things that are done in its constructor. Some of that is assigning pins for a particular sensor library. Thus I need more than one instance of the same library. I could edit the library and move pin assignment to an init function, but that seems like going about it in the wrong way. I have several sensors connected so I need several libraries each with their own instances.If I am pushing the use of inheritance here and this is usually done in a different manner, Id be more than keen to try that. – Misha Feb 22 '21 at 05:03

1 Answers1

1

You don't have constructors for the Base_A class and Base_B class (which takes two integers as arguments). This is exactly what the compiler is showing us with an error. So all what we need to do is define the constructor.

class Base_A : Base 
{
public:
    Base_A(int a, int b) : Base(a, b) {}
};

class Base_B : Base 
{
public:
    Base_B(int a, int b) : Base(a, b) {}
};

Note: You might run into the diamond problem. This is because both Base_A and Base_B are inherited from the class Base.

D-RAJ
  • 3,263
  • 2
  • 6
  • 24
  • This has solved the compiling issue related to the constructor. Thank you! If I may take this a step further; How do I use the methods native to "Base" in "Base_A"? For example, lets say that Base has a method: "void readSensor()". How do I access that method of Base_A within Parent? when I add the following line in the cpp file of Parent.cpp: "Base_A::readSensor()" I get the following error: "void Base::readSensor() is inaccessible". – Misha Feb 22 '21 at 04:58
  • @Misha Its because `Base_A` and `Base_B` has a private inheritance with `Base`. Not only that, if you try to do something like `Base_A::readSensor()` where the function `readSensor()` is not there in `Base_A` but in `Base`, itll throw an error saying that the function is undefined. So try making the inheritance of `Base_A` and `Base` a public one (like this: `class Base_A : public Base { ... };` and call `Base::readSensor()` from `Parent`. – D-RAJ Feb 22 '21 at 05:06
  • 1
    You are a star. Thank you, that sorted out everything. The necessity of making it public is obvious in heinzeit. Thank you for you help, this has solved all the issues I had. Would you say that the above method is the 'most' correct way of getting multiple instance of the same class in "Parent"? – Misha Feb 22 '21 at 05:15
  • @Misha I'm not sure what your actually trying to accomplish here, so I cant say what I would do given this situation. First you have the diamond problem. And if your goal is to have multiple [instances](https://en.wikipedia.org/wiki/Instance_variable) is not the way to go. Also you might wanna look into [Composition over Inheritance](https://en.wikipedia.org/wiki/Composition_over_inheritance). – D-RAJ Feb 22 '21 at 05:31