c类继承,对’Class :: Constructor()’的未定义引用

前端之家收集整理的这篇文章主要介绍了c类继承,对’Class :: Constructor()’的未定义引用前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
编辑:在底部发布固定代码.谢谢大家的帮助!

我只是在学习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,systemindex )
        {
            cout << "----StarSystem " << starsystemindex << ": converting empty SystemBody into Planet " << systembodyindex << endl;
        }
};

class ExitNode : public SystemBody
{
    public:
        ExitNode ( int 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;
}

解决方法

也许这只是我,但你的构造函数是一个简单的declration,没有定义:
class StarSystem
    {
        public:
            StarSystem(); // <--- Undefined!

您已声明了构造函数,但是没有定义此构造函数中实际发生的内容.

如果它是一个什么也不做的构造函数,那就做吧

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

作为旁注,当发布这些类型的东西时,它有助于发布错误号并发表评论或某种指示错误发生的位置(因此我们可以在您的巨大代码中看到它).

编辑:重要的是,如果您根本不使用该构造函数,只需将其删除即可.

原文链接:https://www.f2er.com/c/119380.html

猜你在找的C&C++相关文章