. Advertisement .
..3..
. Advertisement .
..4..
As advised, I used some code samples in another forum, but it could not improve the problem. My question is the “c++ undefined reference to constructor” in cpp-how to solve it? The command line is:
...
#include "GameObjects/StaticObject.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));
...
...
#include <SFML/Graphics.hpp>
#include "GameObjects/StaticObject.hpp"
...
// Objects
StaticObject *background;
...
#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;
};
#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() {
}
#include "GameObject/StaticObject.cpp"
and the result:
main.cpp:20: undefined reference to `StaticObject::StaticObject(Graphics*, sf::String,
sf::Vector2)'
What does the message mean? Can you advise me to fix it? If you have other better answers, leave them in the answer box below.
The cause:
The error occured because the linker was unable to find the definition of a function/method (in this case, constructor).
When you added the cpp file, the compiler caused that code as part of the file (you should not do this).
These are the reasons of this error.
Solution:
You need to link the two objects together after compiling the
GameObject/StaticObject.cpp
code as its own object.Or you can add all of the.cpp files to a single project so that Netbeans can link all of the
.o
files together into a single executable. If you makeStaticObject.cpp
into its own library, you need to specify the path to that library so that the linker can locate the implementation.When you make your own program, it’s really great if these lines appear:
Although, if you specify all the source files (and any libraries) in the same command line, gcc/g++ can bypass all the intermediate.o file generations by many ways and build the
main_program
immediately.The linker could not find
StaticObject
‘s definition, so you have not compiled or linked StaticObject.cpp. Each.cpp file must be separately compiled. The object files that the compiler creates for each one should be provided to the linker.StaticObject.cpp is included in Main.cpp because you tell the preprocessor that it will insert the contents of StaticObject.cpp inside Main.cpp. This allows you to compile the definitions into Main.cpp’s output.