SFML community forums
Help => Graphics => Topic started by: codeed on December 11, 2010, 07:31:52 pm
-
Hi all,
I have problems using sf::Image as static attributes. I've created an empty cmd console Project with Visual C++ 2008 and added the Missile Class from this Tutorial:
http://www.sfml-dev.org/tutorials/1.6/graphics-sprite.php
If I add now a new missile object i get error LNK2020 and error LNK2001.
When i remove the keyword static it works fine.
I've linked all required libs in the project settings and included the Graphics.hpp in the standard header file (stdafx.h).
Main.cpp
// Main.cpp: Hauptprojektdatei.
#include "stdafx.h"
#include "Missile.h"
using namespace System;
int main(array<System::String ^> ^args)
{
Missile missile;
return 0;
}
Missile.h
#pragma once
#include <SFML/Graphics.hpp>
class Missile
{
public :
static bool Init(const std::string& ImageFile)
{
return Image.LoadFromFile(ImageFile);
}
Missile()
{
Sprite.SetImage(Image); // every sprite uses the same unique image
}
private :
static sf::Image Image; // shared by every instance
sf::Sprite Sprite; // one per instance
};
Missile.cpp
#include "StdAfx.h"
#include "Missile.h"
I hope you can help me.
Regards Paul
-
You posted in the wrong subforum, this has nothing to do with the C binding of SFML.
You must define static variables outside the class (in the .cpp file):
sf::Image Missile::Image;
But your code is a little bit strange. First,
int main(array<System::String ^> ^args)
isn't C++, rather C++/CLI. If you don't need this language crucially for .NET interop, you'd better avoid it. The other thing is #include "Missile.h"
inside the header "Missile.h", which makes no sense.
-
Hi Nexus,
thanks for your fast reply. But I don't know how moving the static variable outside of the class definition should solve the problem. I want it to be an attribute of the class.
Anyway I tried your suggestion and put the Image variable above the class.
Now if I start the programme it spits out random letters and numbers and crashes after a few seconds.
PS: I mixed up the file names in my first post the .h file is in fact the .cpp file
-
But I don't know how moving the static variable outside of the class definition should solve the problem. I want it to be an attribute of the class.
Only the definition has to be outside the class. The declaration of the static variable is still inside.
Now if I start the programme it spits out random letters and numbers and crashes after a few seconds.
Did you link the correct libraries (consistent debug/release configuration)? If yes, could you be a bit more specific about your problem? Maybe a minimal, complete code that represents the issue?
-
Now I get it. Also I've linked the wrong graphics lib :oops:.
Thanks for your help !