Undefined reference to class constructor, including .cpp file fixes

前端 未结 6 1623
萌比男神i
萌比男神i 2020-12-24 14:14

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

相关标签:
6条回答
  • 2020-12-24 14:29

    The linker cannot find the definition for StaticObject, which means you have not compiled and linked StaticObject.cpp. Each .cpp file needs to be separately compiled, and the object files the compiler produces for each one needs to be given to the linker.

    The reason including StaticObject.cpp in Main.cpp works is that you are telling the preprocessor to insert the contents of StaticObject.cpp into Main.cpp, which you are compiling, so the definitions become part of Main.cpp's compiled output.

    0 讨论(0)
  • 2020-12-24 14:31

    I had the same issue but mine was because I'm using Eclipse CDT with and Autotools project. So I had to manually add the new .h and .cpp files to the corresponding Makefile.am, and then do a project > reconfigure project, rebuild, an that was it.

    0 讨论(0)
  • 2020-12-24 14:34

    I dont know much of netbeans, but probably GameObject/StaticObject.cpp is not included as part of the sources to compile.

    That's why if you include it manually at main.cpp he found source code and everything is ok.

    0 讨论(0)
  • 2020-12-24 14:42

    I had the same issue in Qt, where I forgot to add the class to the pro file. Apparently, without adding it to the pro file, the compiler doesn't recognize it. So I added it in, and then ran qmake again, and that silved the problem.

    Basically, you need to add the file in the sources.

    0 讨论(0)
  • 2020-12-24 14:44

    including the cpp file causes the compiler to build that code as part of that file (which you should not do), what you need to do is compile the GameObject/StaticObject.cpp file as its own object, and link the 2 objects together.

    0 讨论(0)
  • 2020-12-24 14:52

    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.

    0 讨论(0)
提交回复
热议问题