C++ 类继承,未定义对'Class::Constructor()'的引用

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

本文关键字:Constructor 引用 Class C++ 继承 未定义      更新时间:2023-10-16

编辑:在底部张贴修复代码。谢谢大家的帮助!

我正在学习c++,在继承方面遇到了麻烦。我已经搜索和搜索,并尝试了任何我可以,但我不能得到这个代码来编译,同时保留我想要它的功能。

我觉得我犯了一个愚蠢的错误,或者我只是错过了一些大的概念,但如果有人能看看它,我真的很感激!

如果我注释掉StarSystem构造函数中创建对象的3行,那么它就会编译,所以我知道这与问题有关。

    #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;
    }
编辑:

感谢大家的帮助!只是在这里张贴固定的代码,以防将来有人可能会发现这有用。

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

可能是我的问题,这里的构造函数是一个没有定义的简单声明:

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

你声明了一个构造函数,但是没有定义在这个构造函数中实际发生了什么。

如果它是一个什么都不做的构造函数,则执行

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

作为旁注,当发布这些东西时,它有助于发布错误编号,并在错误发生的地方放置注释或某种指示符(因此我们可以在您的巨大代码团中看到它)。

编辑:

需要注意的是,如果您根本不使用该构造函数,请删除它。

您认为因为没有调用SystemBody(),所以不需要定义它。然而,你是间接调用它。

不要做

SystemBody() {}; 

建议。这不是你想要的。如果您不使用它,请将其完全删除。


你的类Star继承自SystemBody。这意味着当一个新的Star被构造时,SystemBody的构造函数被调用。

这一行

Star(int systembodyindex, int starsystemindex)
    {

实际上被编译为

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

如果你自己不调用SystemBody的默认构造函数,编译器会调用它。


如果你考虑一下,当你创建一个新的Star时,你需要以某种方式初始化SystemBody。你可以显式地像

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

您定义了许多默认构造函数,但没有实现它们。而不是

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

StarSystem(){}
            ^^ this is empty implementation