2

I'm trying to do multiple levels of inheritance and call the base constructor however the complier cannot find my base constructor..

no overloaded function takes 8 arguments

where have I gone wrong in my code?

The .cpp for my base class constructor defaults 10 member variable ints to 0

Baseclass::Baseclass(void) : m_1(0),m_2(0),
                                   m_3(0),m_4(0),
                                   m_x(0),m_y(0),
                                   m_5(0),m_5(0),
                                   m_7(0), m_8(0)
{
}

Baseclass::Baseclass(int _1,int _2,int _3, int _4, int _5,int _6, int _7, int _8)
                                 : m_1(_1),m_2(_2),
                                   m_3(_3),m_4(_4),
                                   m_x(0),m_y(0),
                                   m_5(_5),m_6(_6),
                                   m_7(_7), m_8(_8)
{
}

and in my first subclass header I have

class SubclassA:
    public Baseclass
{
public:
    SubclassA(void);
    using Baseclass::Baseclass;
    ~SubclassA(void);

follow by the second subclass having:

class SubclassB:
    public SubclassA
{
public:
    SubclassB(void);
    using Baseclass::Baseclass;
    ~SubclassB(void);

I would then try to initialise a new instance of this class with:

 SubclassB(Id,0,100,100,10,100,1,1)

I was unsure if using SubclassA::SubclassA; for be valid inside SubclassB but it didn't fix the problem anyhow.

Widz
  • 31
  • 6
  • 1>world.cpp(55): error C2661: `'CWorldEntityPlayerBody::CWorldEntityPlayerBody' : no overloaded function takes 8 arguments` – Widz Jan 09 '13 at 12:38
  • Constructors don't get inherited! – Anurag Kalia Jan 09 '13 at 12:40
  • while I know they don't by default, my reading of the C++11 additions and this question + answer made me think it's possible: http://stackoverflow.com/a/434784/1961167 – Widz Jan 09 '13 at 12:43
  • 1
    Did you check if your compiler supports the C++11 standard? – tmaric Jan 09 '13 at 12:43
  • 5
    I hope your parameters aren't actually called 1, 2, 3 etc. That's just for the example, right? – antonijn Jan 09 '13 at 12:46
  • @tomislav-maric using VS2012 and I can use shared pointers? not sure how to find the specific compilier version – Widz Jan 09 '13 at 12:50
  • @AntonieBlom Yeah I've edited them to clear that up, but they aren't simply _1 in the code – Widz Jan 09 '13 at 12:54
  • 1
    @WillHutchinson in your code snippet you call the class `SubClassB` and in the following error message you write `CWorldEntityPlayerBody`, typo? – AndersK Jan 09 '13 at 12:57
  • @claptrap Nah I had changed the variable & class names to make my question more generalized and I just pasted the errors unedited – Widz Jan 09 '13 at 13:10

2 Answers2

1

If you don't give any arguments, the superclass default constructor is called implicitly. To pass on arguments, you have to make an explicit parameterized constructor of subclass like this -

SubclassA::SubclassA(int a, int b, int c, int d, int e, int f, int g, int h) 
: Baseclass(a,b,c,d,e,f,g,h)
{}

On the same lines, make another constructor for class SubclassB using this constructor of subclassA. You cannot use references to Baseclass as you did in SubclassB because you can only give references to immediate superclass in the hierarchy.

All this has to happen because constructors are not inherited by subclasses in C++ in C++03 standard. In compilers supporting that standard or before, they have to be explicitly called by the subclass' constructor. Since your compiler seems to not support C++11, you have to make do with this now.

(Updated)

Anurag Kalia
  • 4,668
  • 4
  • 21
  • 28
  • 2
    Constructors **do** get inherited in _C++11_ if one does `using SuperBaseClassName::SuperBaseClassName`. The OP's compiler just does not implement it yet – K-ballo Jan 09 '13 at 13:00
  • His compiler does support a notable subset of _C++11_, such subset does not happen to contain _inheriting constructors_ – K-ballo Jan 09 '13 at 13:18
  • His compiler supports flimsy sets of features. I am waiting for C++11 since the day I knew initializer lists would be a part of it. Yet, it is not there in Visual Studio yet. I feverishly hope they don't take much time in revising their compiler, else they would lose a (free-loading) customer. – Anurag Kalia Jan 09 '13 at 22:00
  • His compiler already supports _initializer lists_, is their implementation of the standard library which doesn't yet support them, but you are free to use them with your own types... – K-ballo Jan 09 '13 at 22:04
  • Really?!! *runs away to astonished* (to be continued...) – Anurag Kalia Jan 10 '13 at 02:36
1

From your error message, I guess you're using Visual Studio? It does not support inheriting constructors.

See: http://wiki.apache.org/stdcxx/C%2B%2B0xCompilerSupport

It seems Gcc 4.8 is the only compiler supporting this at the moment.

lethal-guitar
  • 4,438
  • 1
  • 20
  • 40
  • ooh. Constructor inheritance is possible now? I am liking C++11 more and more. I have to check the new documentation. – Anurag Kalia Jan 09 '13 at 13:02
  • Thanks, I guess this has cleared it up! I'll try @AnuragKalia's answer below for a possible workaround – Widz Jan 09 '13 at 13:07