Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c++ class inheritance, undefined reference to 'Class::Constructor()'

Tags:

Edit: Posted fixed code at the bottom. Thanks everyone for your help!

I am just learning c++ and am having trouble with inheritance. I have searched and searched and tried anything I can but I can not get this code to compile while retaining the functionality that I want it to have.

I feel like I am making a stupid mistake or maybe I'm just missing some big concept, but if anyone could take a look at it I'd really appreciate it!

If I comment out the the 3 lines in the StarSystem Constructor that create objects, then it compiles, so I know this has to do with the issue.

    #include <iostream> 
    #include <vector>
    #include <string>
    #include <stdlib.h>
    #include <time.h>

    using namespace std;

    class SystemBody
    {
        public:
            SystemBody();
            int systembodyindex;
            int starsystemindex;

            SystemBody(int systembodyindex, int starsystemindex)
            {
                cout << "StarSystem " << starsystemindex << ": creating empty SystemBody " << systembodyindex << endl;
            }
    };


    class Star : public SystemBody
    {
        public:
            Star();
            string startype;

            Star(int systembodyindex, int starsystemindex)
            {
                cout << "StarSystem " << starsystemindex << ": converting empty SystemBody into Star " << systembodyindex << endl;
            }
    };

    class Planet : public SystemBody
    {
        public:
            Planet();
            string planettype;

            Planet(int systembodyindex, int starsystemindex)
            {
                cout << "StarSystem " << starsystemindex << ": converting empty SystemBody into Planet " << systembodyindex << endl;
            }

    };

    class ExitNode : public SystemBody
    {
        public:
            ExitNode();
            vector<int> connectedindexlist;
            ExitNode(int systembodyindex, int starsystemindex)
            {
                cout << "StarSystem " << starsystemindex << ": converting empty SystemBody into Exit Node " << systembodyindex << endl;
            }


    };


    class StarSystem
    {
        public:
            StarSystem();
            int starsystemindex;
            vector<StarSystem> connectedlist;
            vector<Planet> planetlist;

            StarSystem(int index)
            {
                starsystemindex = index;
                cout << "--Creating StarSystem: " << starsystemindex << endl;
                int numberofbodies = (rand() % 4) + 2;
                    for ( int i = 0; i < numberofbodies; i +=1 )
                    {
                        if ( i == 0 )
                        {
                            Star body(i, starsystemindex);
                        }
                        else if ( i == numberofbodies )
                        {
                            ExitNode body(i, starsystemindex);
                        }
                        else
                        {
                            Planet body(i, starsystemindex);
                        }

                    }

            }

            void addConnection(StarSystem connectedstarsystem)
            {
                cout << "--StarSystem " << starsystemindex << ": Adding connection to StarSystem " << connectedstarsystem.starsystemindex << endl;
                connectedlist.push_back(connectedstarsystem);
            }

    };



    int main()
    {
        srand(time(0));
        StarSystem starsystem0(0);
        return 0;
    }

EDIT:

thanks to everyone for your help! just posting the fixed code here in case any one in the future might find this useful.

#include <iostream> 
#include <vector>
#include <string>
#include <stdlib.h>
#include <time.h>

using namespace std;

class SystemBody
{
    public:
        int systembodyindex;
        int starsystemindex;
        SystemBody ( )
        {
            cout << "----SystemBody BEING CREATED WITH NO PARAMETERS" << endl;
        }
        SystemBody ( int bodyindex, int systemindex )
        {
            systembodyindex = bodyindex;
            starsystemindex = systemindex;
            cout << "----StarSystem " << starsystemindex << ": creating empty SystemBody " << systembodyindex << endl;
        }

};


class Star : public SystemBody
{
    public:
        Star ( int bodyindex, int systemindex ) : SystemBody ( bodyindex, systemindex )
        {
            cout << "----StarSystem " << starsystemindex << ": converting empty SystemBody into Star " << systembodyindex << endl;
        }
};


class Planet : public SystemBody
{
    public:
        Planet ( int bodyindex, int systemindex ) : SystemBody ( bodyindex, systemindex )
        {
            cout << "----StarSystem " << starsystemindex << ": converting empty SystemBody into Planet " << systembodyindex << endl;
        }
};

class ExitNode : public SystemBody
{
    public:
        ExitNode ( int bodyindex, int systemindex ) : SystemBody ( bodyindex, systemindex )
        {
            cout << "----StarSystem " << starsystemindex << ": converting empty SystemBody into ExitNode " << systembodyindex << endl;
        }
};


class StarSystem
{
    public:
        int starsystemindex;
        vector<StarSystem> connectedlist;
        vector<Planet> planetlist;

        StarSystem ( int index )
        {
            starsystemindex = index;
            cout << "--Creating StarSystem: " << starsystemindex << endl;
            int numberofbodies = (rand() % 4 ) + 2;
            for ( int i = 0; i <= numberofbodies; i +=1 )
            {
                if ( i == 0)
                {
                    Star body(i, starsystemindex);
                }
                else if ( i == numberofbodies )
                {
                    ExitNode body(i, starsystemindex);
                }
                else
                {
                    Planet body(i, starsystemindex);
                }
            }
        }
};

int main()
{

    srand(time(0));
    StarSystem starsystem0(0);
    return 0;
}