简单的 SFML/C++问题,对文字字符串和静态成员感到困惑

发布于 2024-11-14 03:02:05 字数 1126 浏览 2 评论 0原文

我对 http://www.sfml 的代码有点困惑-dev.org/tutorials/1.6/graphics-sprite.php

即底部详细说明类“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
};

我正在尝试使用“Init”将文件加载到班级。我正在尝试这样做:

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

然后继续声明该类的对象。然而,我遇到了又长又详细的错误,这让我觉得我不应该在那里放一个字符串,或者我错过了一些基本的东西。我对 C++ 有点陌生,所以语法仍然让我感到困惑,我已经研究了很长一段时间但无法弄清楚。我尝试过调用指针等,但我真的不知道下一步该怎么做。

编辑:我收到的错误是:

main.o: 在函数 Ship::Init(std::basic_string, std::allocator > const&) 中: main.cpp:(.text._ZN4Ship4InitERKSs[Ship::Init(std::basic_string, std::allocator > const&)]+0x10): 对 Ship::Image 的未定义引用 main.o:在函数 Ship::Ship() 中: main.cpp:(.text._ZN4ShipC2Ev[_ZN4ShipC5Ev]+0x19): 对 Ship::Image 的未定义引用

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
};

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

南城旧梦 2024-11-21 03:02:05

从术语“长详细错误”来看,我只是猜测您可能对链接器错误感到困惑。这是因为,您可能忘记

static sf::Image Image;

.cpp 文件中定义,或者忘记将该 .cpp 文件与包含该文件的编译链接起来。
全局范围的适当.cpp 文件中定义您的static 成员。

sf::Image Missile::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;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文