Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Visual C++ 2008: sf::Image as static attribute  (Read 2644 times)

0 Members and 1 Guest are viewing this topic.

codeed

  • Newbie
  • *
  • Posts: 3
    • View Profile
Visual C++ 2008: sf::Image as static attribute
« 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

Code: [Select]

// Main.cpp: Hauptprojektdatei.

#include "stdafx.h"
#include "Missile.h"

using namespace System;

int main(array<System::String ^> ^args)
{
     Missile missile;

    return 0;
}



Missile.h

Code: [Select]

#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

Code: [Select]

#include "StdAfx.h"
#include "Missile.h"


I hope you can help me.

Regards Paul

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Visual C++ 2008: sf::Image as static attribute
« Reply #1 on: December 11, 2010, 08:15:35 pm »
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):
Code: [Select]
sf::Image Missile::Image;

But your code is a little bit strange. First,
Code: [Select]
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
Code: [Select]
#include "Missile.h" inside the header "Missile.h", which makes no sense.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

codeed

  • Newbie
  • *
  • Posts: 3
    • View Profile
Visual C++ 2008: sf::Image as static attribute
« Reply #2 on: December 11, 2010, 09:15:15 pm »
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

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Visual C++ 2008: sf::Image as static attribute
« Reply #3 on: December 11, 2010, 09:44:30 pm »
Quote from: "codeed"
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.

Quote from: "codeed"
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?
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

codeed

  • Newbie
  • *
  • Posts: 3
    • View Profile
Visual C++ 2008: sf::Image as static attribute
« Reply #4 on: December 12, 2010, 02:45:01 am »
Now I get it. Also I've linked the wrong graphics lib :oops:.

Thanks for your help !

 

anything