Skip to main content
gamedev.net
🔒 Locked 🤖 Godot

Static function error

Started by zoner7 4 replies 1.4k views
Original Post
zoner7
zoner7
I've got a fun little compiler error and have absolutely no idea how to fix it. I have a class called VariableLabel detailed below. Note that the sf namespace refers to SFML.

////////////VariableLabel.h#include "headers.h"#pragma onceclass VariableLabel{public:	VariableLabel();	static bool Initialize(const std::string& ImageFile);private:    static sf::Image image;      // Each sprite object shares this single image    sf::Sprite Sprite;           // But there is one sprite per variable label	double value;};////////////VariableLabel.cpp#include "VariableLabel.h"VariableLabel::VariableLabel(){	Sprite.SetImage(image); // Every sprite uses the same image}bool VariableLabel::Initialize(const std::string& ImageFile){	return image.LoadFromFile(ImageFile);}


If I comment out all the functions, everything works. But if I even decide to include the definition of the constructor in the .h file (while still leaving its implementation in the .cpp file commented out), the following error is produced.

The compiler is telling me 1>Simulation.obj : error LNK2019: unresolved external symbol "public: __thiscall VariableLabel::VariableLabel(void)" (??0VariableLabel@@QAE@XZ) referenced in function "public: __thiscall Simulation::Simulation(class std::basic_string,class std::allocator >,int,int,int)" (??0Simulation@@QAE@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@HHH@Z)

Instances of VariableLabel are declared as private in my class called Simulation.

On another note, I have another random question regarding my implementation of the constructor. If I declare an instance of VariableLabel before called the static function "static bool Initialize(const std::string& ImageFile)", would I receive an error? I ask this because the constructor would otherwise be accessing the variable image when it is has not been initialized.

Thank you.

-Nick
Evil Steve
Evil Steve
Quote:
Original post by zoner7

The compiler is telling me 1>Simulation.obj : error LNK2019: unresolved external symbol "public: __thiscall VariableLabel::VariableLabel(void)" (??0VariableLabel@@QAE@XZ) referenced in function "public: __thiscall Simulation::Simulation(class std::basic_string,class std::allocator >,int,int,int)" (??0Simulation@@QAE@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@HHH@Z)

That errors is the compiler saying it can't find an implementation for the function VariableLabel::VariableLabel() - I.e. the VariableLabel constructor. Is VariableLabel.cpp definitely being compiled?

EDIT: I missed this bit "But if I even decide to include the definition of the constructor in the .h file (while still leaving its implementation in the .cpp file commented out), the following error is produced." - Well, I'm not surprised it doesn't work, where did you expect the compiler to get the implementation for your constructor from?

Quote:
Original post by zoner7
On another note, I have another random question regarding my implementation of the constructor. If I declare an instance of VariableLabel before called the static function "static bool Initialize(const std::string& ImageFile)", would I receive an error? I ask this because the constructor would otherwise be accessing the variable image when it is has not been initialized.
That depends what the default constructor for sf::Image is, and how the class works. If it's illegal to use a sf::Image without calling LoadFromFile() or similar on it, then you'll probably get an error at runtime - but it really depends on the SFML library.
michaweyel
michaweyel
What this is basically telling you is that the linker couldn't find the implementation of the constructor of your VariableLabel class. Did you also comment out everything in you .cpp file?

Also, why did you comment everything out in the first place?
zoner7
zoner7
Quote:
Original post by Evil Steve
.e. the VariableLabel constructor. Is VariableLabel.cpp definitely being compiled?

EDIT: I missed this bit "But if I even decide to include the definition of the constructor in the .h file (while still leaving its implementation in the .cpp file commented out), the following error is produced." - Well, I'm not surprised it doesn't work, where did you expect the compiler to get the implementation for your constructor from?


Let me clarify this bit. I was commenting sections of the code in an effort to isolate the cause of the compiler error. The error occurs when the code is not commented.

Here is the sequence of messages received during the compilation of the solution. VariableLabel.cpp supposedly compiles successfully:

1>------ Rebuild All started: Project: Basebal sim, Configuration: Debug Win32 ------
1> VariableLabel.cpp
1> Simulation.cpp
1> Pitch.cpp
1> Main.cpp
1> Environments.cpp
1> DragCoefficient.cpp
1> Ball.cpp
1> Generating Code...
1>VariableLabel.obj : error LNK2001: unresolved external symbol "private: static class sf::Image VariableLabel::image" (?image@VariableLabel@@0VImage@sf@@A)
1>C:\Users\Nick\Documents\Visual Studio 2010\Projects\Basebal sim\Debug\Basebal sim.exe : fatal error LNK1120: 1 unresolved externals
Evil Steve
Evil Steve
Static member variables need initialised. In VariableLabel.cpp add:
sf::Image VariableLabel::image;
zoner7
zoner7
Quote:
Original post by Evil Steve
Static member variables need initialised. In VariableLabel.cpp add:
sf::Image VariableLabel::image;


That resolved the problem. Thank you very much.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.