c ++类继承,未定义引用'Class :: Constructor()' [英] c++ class inheritance, undefined reference to 'Class::Constructor()'

查看:185
本文介绍了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< :创建空SystemBody< systembodyindex< endl
}
};


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

Star(int systembodyindex,int starsystemindex)
{
cout< StarSystem<< starsystemindex< :将空SystemBody转换为星< systembodyindex< endl
}
};

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

Planet(int systembodyindex,int starsystemindex)
{
cout< StarSystem<< starsystemindex< :将空SystemBody转换为Planet< systembodyindex< endl
}

};

class ExitNode:public SystemBody
{
public:
ExitNode();
vector< int>连接索引
ExitNode(int systembodyindex,int starsystemindex)
{
cout< StarSystem<< starsystemindex< :将空SystemBody转换成退出节点< systembodyindex< endl
}


};


class StarSystem
{
public:
StarSystem();
int starsystemindex;
vector< StarSystem>连接列表
vector< Planet> planetlist是什么意思

StarSystem(int index)
{
starsystemindex = index;
cout< - 创建StarSystem:< starsystemindex< endl
int numberofbodies =(rand()%4)+ 2;
for(int i = 0; i< numberofbodies; i + = 1)
{
if(i == 0)
{
星体,starsystemindex);
}
else if(i == numberofbodies)
{
ExitNode body(i,starsystemindex);
}
else
{
星球体(i,starsystemindex);
}

}

}

void addConnection(StarSystem connectedstarsystem)
{
cout< ; --StarSystem< starsystemindex< :添加到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<< ----系统体无创建参数< endl
}
SystemBody(int bodyindex,int systemindex)
{
systembodyindex = bodyindex;
starsystemindex = systemindex;
cout<< ----星系统< starsystemindex< :创建空SystemBody< systembodyindex< endl
}

};


class Star:public SystemBody
{
public:
Star(int bodyindex,int systemindex):SystemBody(bodyindex,systemindex)
{
cout<< ----星系统< starsystemindex< :将空SystemBody转换为星< systembodyindex< endl
}
};


class Planet:public SystemBody
{
public:
Planet(int bodyindex,int systemindex):SystemBody(bodyindex,systemindex)
{
cout<< ----星系统< starsystemindex< :将空SystemBody转换为Planet< systembodyindex< endl
}
};

class ExitNode:public SystemBody
{
public:
ExitNode(int bodyindex,int systemindex):SystemBody(bodyindex,systemindex)
{
cout<< ----星系统< starsystemindex< :将空SystemBody转换成ExitNode< systembodyindex< endl
}
};


class StarSystem
{
public:
int starsystemindex;
vector< StarSystem>连接表
vector< Planet> planetlist是什么意思

StarSystem(int index)
{
starsystemindex = index;
cout<< - 创建StarSystem:< starsystemindex< endl
int numberofbodies =(rand()%4)+ 2;
for(int i = 0; i <= numberofbodies; i + = 1)
{
if(i == 0)
{
星体i,starsystemindex);
}
else if(i == numberofbodies)
{
ExitNode body(i,starsystemindex);
}
else
{
星球体(i,starsystemindex);
}
}
}
};

int main()
{

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


解决方案

也许只是我,您的构造函数在这里是一个没有定义的简单的声明:

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

你有一个构造函数声明,但是没有定义这个构造函数的实际内容。 p>

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

  StarSystem {} //定义。 
//内部没有发生,但是一切都得到默认构造!

注意,当发布这些类型的东西时,它有助于发布错误号,一个注释或某种指示错误发生的地方(所以我们可以看到它在你的巨大的blob代码)。



编辑:
请注意,如果你根本不使用该构造函数,只需删除它。


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

解决方案

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.

这篇关于c ++类继承,未定义引用'Class :: Constructor()'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆