c++ class inheritance, undefined reference to '

2019-06-16 04:37发布

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;
}

3条回答
爷的心禁止访问
2楼-- · 2019-06-16 05:32

Perhaps it's just me, but your Constructor here is a simple declration that's not been defined:

class StarSystem
    {
        public:
            StarSystem(); // <--- Undefined!

You have a constructor declared, but there's no definition of what's actually going on in this constructor.

If it's a constructor that just does nothing, do

StarSystem () {} // Defined.
// Nothing happens inside, but everything gets default-constructed!

As a side note, when posting these kinds of things, it helps to post the error number and put a comment or some kind of indicator of where the error is happening (so we can see it in your giant blob of code).

EDIT: As an important note, if you're not using that constructor at all, just delete it.

查看更多
家丑人穷心不美
3楼-- · 2019-06-16 05:33

You think because you are not calling SystemBody() you don't need to define it. However you are calling it indirectly.

And do't do

SystemBody() {}; 

as suggested. This is not what you want. Instead remove it completely if you don't use it.


Your class Star inherits from SystemBody. That means when a new Star is constructed the constructor for SystemBody is called.

This line

Star(int systembodyindex, int starsystemindex)
    {

is actually compiled to

Star(int systembodyindex, int starsystemindex) :
    SystemBody()   // Here
    {

The compiler calls the default constructor for SystemBody if you don't call one yourself.


If you think about it you need to initialize SystemBody somehow when you create a new Star. You can do this explicitly like

Star(int systembodyindex, int starsystemindex) :
    SystemBody(systembodyindex)   // Here
    {
查看更多
Deceive 欺骗
4楼-- · 2019-06-16 05:33

you defined many default constructors but didn't implement them. Instead of

StarSystem(); // <- it is OK if you implement this somewhere but you didn't

write

StarSystem(){}
            ^^ this is empty implementation
查看更多
登录 后发表回答