开发者

Simple SFML/C++ question, confused about literal strings and static members

开发者 https://www.devze.com 2023-03-11 02:36 出处:网络
I\'m a bit confused by the code at http://www.sfml-dev.org/tutorials/1.6/graphics-sprite.php Namely the code at the bottom detailing the class \"Missile\":

I'm a bit confused by the code at http://www.sfml-dev.org/tutorials/1.6/graphics-sprite.php

Namely the code at the bottom detailing the class "Missile":

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开发者_如何学Go
};

I am trying to use "Init" to load a file to the private image member of the class. I am trying to do this with:

if (!Missile::Init("missile.bmp")) return EXIT_FAILURE;\

then proceed to declare an object of that class. However, I am getting long, verbose errors that make me think that I should not be putting a string there, or that I am missing something fundamental. I'm a bit new to C++ so the syntax is still confusing me, I've looked at this for quite a while and can't figure it out. I've tried calling pointers, etc, but I really don't know what to do next.

Edit: The error I'm getting is:

main.o: In function Ship::Init(std::basic_string, std::allocator > const&): main.cpp:(.text._ZN4Ship4InitERKSs[Ship::Init(std::basic_string, std::allocator > const&)]+0x10): undefined reference to Ship::Image main.o: In function Ship::Ship(): main.cpp:(.text._ZN4ShipC2Ev[_ZN4ShipC5Ev]+0x19): undefined reference to Ship::Image


From the term, long verbose error, I am just guessing that you might have got confused with linker error. That's because, you might have either forgot to define,

static sf::Image Image;

in a .cpp file or forgot to link that .cpp file with your compilation where it's included. Define your static member in appropriate .cpp file in global scope.

sf::Image Missile::Image;
0

精彩评论

暂无评论...
验证码 换一张
取 消