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

Posted

技术标签:

【中文标题】c++类继承,未定义引用\'Class::Constructor()\'【英文标题】:c++ class inheritance, undefined reference to 'Class::Constructor()'c++类继承,未定义引用'Class::Constructor()' 【发布时间】:2013-04-14 22:56:13 【问题描述】:

编辑:在底部发布了固定代码。感谢大家的帮助!

我刚刚学习 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;

【问题讨论】:

您是否愿意指定您希望它具有什么功能? 我正在尝试创建一个通用的 systembody 类,该类将具有继承类 star、plane 和 exitnode 共有的成员属性和方法。 在构造函数中,您将 StarExitNodePlanet 对象初始化为基于堆栈的自动变量 - 一旦超出范围,这些对象就会全部被删除。您是否打算将这些添加到某种列表或向量中? 它们确实被添加到另一个类的向量中,我试图简单地编写这篇文章的代码。 【参考方案1】:

也许只有我一个人,但这里的构造函数是一个尚未定义的简单声明:

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

你声明了一个构造函数,但没有定义这个构造函数中实际发生的事情。

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

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

附带说明一下,在发布此类内容时,发布错误编号并添加注释或某种指示错误发生位置的指示符会有所帮助(因此我们可以在您的巨大代码块中看到它) .

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

【讨论】:

@zacharydimaria 不,这是一个正确的定义。但是,您只有 2 个构造函数(或者至少 C++ 认为您有),因为您将 StarSystem(); 浮动在顶部。如果您不希望它成为构造函数,请将其从代码中完全删除。【参考方案2】:

你认为因为你没有调用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
    

【讨论】:

我以为我的 Star 类继承了 SystemBody,而不是 StarSystem? @zacharydimaria 哦,我的错。我会编辑它。但情况是一样的。【参考方案3】:

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

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

StarSystem()
            ^^ this is empty implementation

【讨论】:

以上是关于c++类继承,未定义引用'Class::Constructor()'的主要内容,如果未能解决你的问题,请参考以下文章

继承makefile C++未定义符号首先被引用

未定义的引用,继承问题 - C++ [重复]

c ++继承编译问题未定义对派生类的引用[重复]

C ++未定义对已继承的受保护类成员的引用[重复]

c++ 继承问题:未定义对“vtable”的引用

C++ 未定义的抽象类构造函数