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

Author Topic: [SOLVED] Static sf::Image and sf::Sprite?  (Read 3237 times)

0 Members and 1 Guest are viewing this topic.

Pyrius

  • Newbie
  • *
  • Posts: 39
    • View Profile
[SOLVED] Static sf::Image and sf::Sprite?
« on: October 18, 2011, 12:39:36 pm »
I have a class which handles all of the drawing so far, so it has sf::Image's and sf::Sprite's. Those objects will always be the same, so I want to declare them static instead of loading the images and giving the sprites images in the constructor and accessing them with an object of that class. I want to do something like this:

Code: [Select]

class Foo
{
public:
      static void loadImages();

private:
      static sf::Image image;
      static sf::Sprite sprite;
};


Then in loadImages():

Code: [Select]

void Foo::loadImages()
{
     Foo::image.LoadFromFile("image.png");
     Foo::sprite.SetImage(image);
}


But I can't compile it. I get the following errors:

"undefined reference to 'Foo::image'"
"undefined reference to 'Foo::sprite'"

I'm still learning about the static keyword, so I guess it's just a syntax error.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
[SOLVED] Static sf::Image and sf::Sprite?
« Reply #1 on: October 18, 2011, 12:55:27 pm »
Quote
I'm still learning about the static keyword

Google is often more helpful (and quick) than posting on a forum and waiting for an answer, especially for such a basic C++ problem ;)
http://tinyurl.com/692z3ds
Laurent Gomila - SFML developer

Pyrius

  • Newbie
  • *
  • Posts: 39
    • View Profile
[SOLVED] Static sf::Image and sf::Sprite?
« Reply #2 on: October 18, 2011, 01:11:48 pm »
Hehe lmgtfy.com is awesome. I tried googling it but didn't get an answer. I didn't think about googling "undefined reference" for some reason...

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
[SOLVED] Static sf::Image and sf::Sprite?
« Reply #3 on: October 18, 2011, 01:46:08 pm »
Don't you have a C++ book you learn with? If not, you should really buy or borrow a good one (like the C++ Primer), so that basics like the static keyword get explained in details. This is much more effective than when you have to google everything, and it leaves less knowledge gaps ;)
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Pyrius

  • Newbie
  • *
  • Posts: 39
    • View Profile
[SOLVED] Static sf::Image and sf::Sprite?
« Reply #4 on: October 18, 2011, 02:57:35 pm »
Nope, I don't; I learned the basics through TheNewBoston (thenewboston.com), and I didn't learn static. Or, maybe I did, and I forgot about it.

EDIT: I give myself challenge programs to complete and when I need to know how to do something for that program that I don't know yet I look it up, and if I can't find an answer, I use the forum on cplusplus.com to ask for an answer.

julen26

  • Jr. Member
  • **
  • Posts: 89
    • View Profile
    • http://julen26.blogspot.com
[SOLVED] Static sf::Image and sf::Sprite?
« Reply #5 on: October 18, 2011, 11:35:59 pm »
You also should declare the static members outside the class.

Foo.cpp
Code: [Select]
sf::Image Foo::image;
sf::Sprite Foo::sprite;

sbroadfoot90

  • Jr. Member
  • **
  • Posts: 77
    • View Profile
[SOLVED] Static sf::Image and sf::Sprite?
« Reply #6 on: October 19, 2011, 12:11:57 am »
Quote from: "julen26"
You also should declare the static members outside the class.

Foo.cpp
Code: [Select]
sf::Image Foo::image;
sf::Sprite Foo::sprite;


No, they should be declared inside the class in the interface file Foo.h

Code: [Select]
class Foo {
private:
   static sf::Image image;
   static sf::Sprite sprite;
}


and then defined (and initialised via default ctors) in the implementation file Foo.cpp

Code: [Select]
sf::Image Foo::image;
sf::Sprite Foo::sprite;


Declaration is where the name and type of a something is made known to the compiler.

Definition (for objects) is where memory is set aside for the variable.

Pyrius

  • Newbie
  • *
  • Posts: 39
    • View Profile
[SOLVED] Static sf::Image and sf::Sprite?
« Reply #7 on: October 19, 2011, 12:38:46 pm »
Yup, I found that out :). Thanks for responding.