对类构造函数的未定义引用,包括.cpp文件修复 [英] Undefined reference to class constructor, including .cpp file fixes

查看:267
本文介绍了对类构造函数的未定义引用,包括.cpp文件修复的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我遇到的问题是,当我为创建的类调用构造函数时,出现以下错误。 main.cpp:20:未定义的引用
`StaticObject :: StaticObject(Graphics *,sf :: String,

sf :: Vector2)'
blockquote>

这个问题可以通过在main.cpp中为.cpp文件添加一个include来解决。

 ... 
#includeGameObjects / StaticObject.cpp
...

尽管解决了这个问题,但这似乎是一个糟糕的解决方案,并且违背了我之前所说的。有没有其他办法可以解决这个问题?我使用Netbeans 7.3和g ++来编写/编译这个程序。以下是相关的代码。



main.cpp

  ... 

#include< SFML / Graphics.hpp>
#includeGraphics / Graphics.hpp
#includeGameObjects / StaticObject.hpp

int main(int argc,char ** argv){

// SETUP
图形图形;

background = new StaticObject(& graphics,Data / Images / BackgroundPlaceholder.png,sf :: Vector2f(0,0));

...

main.hpp

  ... 

#include< SFML / Graphics.hpp>
#includeGameObjects / StaticObject.hpp

...

//对象
StaticObject * background;
...

StaticObject.hpp

  #include< SFML / Graphics.hpp> 
#include../Graphics/Graphics.hpp
$ b $ class StaticObject {
public:
StaticObject();
StaticObject(Graphics * _graphics,sf :: String texture_filename,sf :: Vector2f _position);
StaticObject(const StaticObject& orig);
virtual〜StaticObject();
private:
//精灵存储位置
sf :: Sprite * sprite;
sf :: Texture * texture;
};

StaticObject.cpp

  #includeStaticObject.hpp
#include< SFML / Graphics.hpp>
#include../Graphics/Graphics.hpp

StaticObject :: StaticObject(){
}

StaticObject :: StaticObject(Graphics * _graphics,sf :: String texture_filename,sf :: Vector2f _position){
sprite = _graphics-> AddSprite(texture_filename);
sprite-> setPosition(_position);


StaticObject :: StaticObject(const StaticObject& orig){
}

StaticObject ::〜StaticObject(){
}

如果将以下行添加到main.cpp,则错误消失。

  #includeGameObject / StaticObject.cpp

任何人都可以解释:


  1. 为什么这样可以解决问题? / p>


  2. 为什么.cpp不是通过包含.hpp
    文件隐式包含的?


  3. 是否有更好的方法来做到这一点?

  4. 未定义的引用错误表示链接器未找到函数/方法的定义(即构造函数)。

      StaticObject :: StaticObject(Graphics *,sf :: String,sf :: Vector2< float>)

    以及添加以下行的原因:

      #includeGameObject / StaticObject.cpp

    解决了这个问题,是否将实现作为 main.cpp 的一部分引入,而您的实际实现位于 StaticObject中。 CPP 。这是修复这个问题的 不正确的方式

    我没有使用Netbeans,但应该可以将所有.cpp文件添加到单个项目中,以便Netbeans负责将所有 .o 文件链接到一个可执行文件中。



    如果 StaticObject.cpp 内置到它自己的库中(我非常怀疑这是这种情况),那么你可能有指定该库位置的路径,以便链接器可以找到实现。



    这是在构建程序时理想的情况:

     编译:StaticObject.cpp  - > StaticObject.o 
    编译:main.cpp - > main.o
    链接:StaticObject.o,main.o - > main_program

    尽管gcc / g ++可以跳过所有的中间.o文件并直接生成 main_program ,如果您在同一命令行中指定所有源文件(和任何库)。


    The problem I am having is that, when I call a constructor for a class I have created I get the following error.

    main.cpp:20: undefined reference to `StaticObject::StaticObject(Graphics*, sf::String,
    sf::Vector2)'

    This problem can be 'fixed' adding an include for the .cpp file in main.cpp like so.

    ...
    #include "GameObjects/StaticObject.cpp"
    ...
    

    Although this solves the problem, this seems like a poor solution and goes against what I have been previously told. Is there any other way to solve this problem? I'm using Netbeans 7.3 with g++ to code/compile this program. Below is the relevant code.

    main.cpp

    ...
    
    #include <SFML/Graphics.hpp>
    #include "Graphics/Graphics.hpp"
    #include "GameObjects/StaticObject.hpp"
    
    int main(int argc, char** argv) {
    
        //SETUP
        Graphics graphics;
    
        background = new StaticObject(&graphics, "Data/Images/BackgroundPlaceholder.png",  sf::Vector2f(0,0));
    
    ...
    

    main.hpp

    ...
    
    #include <SFML/Graphics.hpp>
    #include "GameObjects/StaticObject.hpp"
    
    ...
    
    // Objects
    StaticObject *background;
    ...
    

    StaticObject.hpp

    #include <SFML/Graphics.hpp>
    #include "../Graphics/Graphics.hpp"
    
    class StaticObject{
    public:
        StaticObject();
        StaticObject(Graphics *_graphics, sf::String texture_filename, sf::Vector2f _position);
        StaticObject(const StaticObject& orig);
        virtual ~StaticObject();
    private:
        // The sprite stores the position
        sf::Sprite *sprite;
        sf::Texture *texture;
    };
    

    StaticObject.cpp

    #include "StaticObject.hpp"
    #include <SFML/Graphics.hpp>
    #include "../Graphics/Graphics.hpp"
    
    StaticObject::StaticObject(){    
    }
    
    StaticObject::StaticObject(Graphics *_graphics, sf::String texture_filename, sf::Vector2f _position) {
        sprite = _graphics->AddSprite(texture_filename);
        sprite->setPosition(_position);
    }
    
    StaticObject::StaticObject(const StaticObject& orig) {
    }
    
    StaticObject::~StaticObject() {
    }
    

    If I add the following line to main.cpp, the error disappears.

    #include "GameObject/StaticObject.cpp"
    

    Can anyone please explain:

    1. Why this fixes the problem?

    2. Why the .cpp was not implicitly included through including the .hpp file?

    3. Is there a better way of doing this?

    解决方案

    The undefined reference error indicates that the definition of a function/method (i.e constructor here) was not found by the linker.

    StaticObject::StaticObject(Graphics*, sf::String,    sf::Vector2<float>)
    

    And the reason that adding the following line:

    #include "GameObject/StaticObject.cpp"
    

    fixes the issue, is it brings in the implementation as part of the main.cpp whereas your actual implementation is in StaticObject.cpp. This is an incorrect way to fix this problem.

    I haven't used Netbeans much, but there should be an option to add all the .cpp files into a single project, so that Netbeans takes care of linking all the .o files into a single executable.

    If StaticObject.cpp is built into a library of its own (I highly doubt that is the case here), then you might have to specify the path to the location of this library, so that the linker can find the implementation.

    This is what ideally happens when you build your program:

    Compile: StaticObject.cpp -> StaticObject.o
    Compile: main.cpp -> main.o
    Link: StaticObject.o, main.o -> main_program
    

    Although there are ways in gcc/g++ to skip all the intermediate .o file generations and directly generate the main_program, if you specify all the source files (and any libraries) in the same command line.

    这篇关于对类构造函数的未定义引用,包括.cpp文件修复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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