0

This is the code I'm trying to understand. It has no specific use. I'm just trying to understand what happens.

#include<iostream>
using namespace std;

class derivedClass;
class baseClass
{
public:
    int objID;
    derivedClass* dcObjPtr;

    baseClass()
    {
        cout << "(1) Default constructor" << objID << endl;
    }

    baseClass(int ID)
    {
        objID = ID;
        dcObjPtr = new derivedClass(1);
        cout << "(2) Constructing base object with ID: " << objID << endl;
    }
};

class derivedClass : public baseClass
{
public:
    derivedClass()
    {}

    derivedClass(int ID) : baseClass(ID)
    {
        cout << "(4) Constructing derived object with ID: " << objID << endl;
    }
};


int main(int argc, char** argv)
{
    derivedClass dcObj(1);

    return 0;
}

Using VS2013 I get the error "derivedClass: class has no constructors", which I don't think is the right error.

I was first using a derivedClass instance, not a pointer. And that was giving me a weird error about a semicolon. Then I saw this post where the accepted answer was to have a pointer, instead of an instance.

These are the questions I have:

  1. Why is the compiler complaining about no constructors. The class clearly has constructors.
  2. How would this work? When we create an instance of dervedClass, there is a baseClass part in it. And this baseClass part has a derivedClass in it, and so on. This is recursive. I would assume if it works it would result in a segmentation fault or something like that. Am I right to assume that this would be recursive (if it works)?

Cheers.

Community
  • 1
  • 1
madu
  • 5,232
  • 14
  • 56
  • 96
  • 2
    In order to say `dcObjPtr = new derivedClass(1);` you need the class definition, which you don't have at that point. If you "fix" that you'll get an infinite recursion. – juanchopanza Jan 12 '15 at 14:04

1 Answers1

1

You're making use of the definition of derivedClass before it's defined, by calling new derivedClass in the baseClass constructor. What you need to do is move the constructor definition outside baseClass and after derivedClass, like so:

#include<iostream>
using namespace std;

class derivedClass;
class baseClass
{
public:
    int objID;
    derivedClass* dcObjPtr;

    baseClass()
    {
        cout << "(1) Default constructor" << objID << endl;
    }

    baseClass(int ID);

};

class derivedClass : public baseClass
{
public:
    derivedClass()
    {}

    derivedClass(int ID) : baseClass(ID)
    {
        cout << "(4) Constructing derived object with ID: " << objID << endl;
    }
};

baseClass::baseClass(int ID)
{
    objID = ID;
    dcObjPtr = new derivedClass(1);
    cout << "(2) Constructing base object with ID: " << objID << endl;
}

int main(int argc, char** argv)
{
    derivedClass dcObj(1);

    return 0;
}    
Chowlett
  • 45,935
  • 20
  • 116
  • 150
  • 1
    I note that running this [on ideone](http://ideone.com/hOzQKD) compiles, but hits a runtime error. I haven't investigated what that is. – Chowlett Jan 12 '15 at 14:06
  • Ah, of course. It's the infinite recursion juanchopanza mentions. – Chowlett Jan 12 '15 at 14:07
  • @Chowlett Thank you. But why can't the compiler say so? If the definition is missing, shouldn't there be a linker error? – madu Jan 12 '15 at 14:13
  • 1
    @madu It _does_ say so. Sort of. At the line the error is on - the line in baseClass::baseClass that calls `new derivedClass` - the only information the compiler has about `derivedClass` is the forward declaration. So, as far as the compiler knows _at that point_, `derivedClass` has no constructors. – Chowlett Jan 13 '15 at 11:55