尝试使用静态方法/成员

发布于 2024-09-13 10:03:42 字数 1904 浏览 0 评论 0原文

过去几年我一直被 C# 编码宠坏了,现在我又回到了 C++,发现我在处理本应简单的东西时遇到了麻烦。我正在使用名为 DarkGDK 的第三方游戏开发库(任何以 db 为前缀的命令),但是 DGDK 不是问题。

这是我的代码:

System.h

#pragma once

#include <string>
#include <map>
#include "DarkGDK.h"

using namespace std;

class System
{
public:
    System();
    ~System();
    void Initialize();

    static void LoadImage(string fileName, string id);
    static int GetImage(string id);

private:
    map<string, int> m_images;
};

System.cpp

#include "System.h"

System::System()
{
}

System::~System()
{
}

void System::Initialize()
{
    dbSetDisplayMode (1024, 640, 32);
    dbSetWindowTitle ("the Laboratory");
    dbSetWindowPosition(100, 10);

    dbSyncOn         ();
    dbSyncRate       (60);

    dbRandomize(dbTimer());
}

void System::LoadImage(string fileName, string id)
{
    int i = 1;

    while (dbImageExist(i))
    {
        i++;
    }

    dbLoadImage(const_cast<char*>(fileName.c_str()), i, 1);
    m_images[id] = i;
}

int System::GetImage(string id)
{
    return m_images[id];
}

这里的想法是有一个将字符串映射到整数值的映射,以使用字符串而不是硬编码值访问图像。这个类还没有完成,所以它不处理像卸载图像这样的事情。我想在不传递 System 实例的情况下访问图像方法,因此我使用了 static。

现在我得到这个错误:

blahblah\system.cpp(39):错误 C2677: 二进制“[”:找不到全局运算符 它采用类型 'std::string' (或 没有可接受的转换)

如果我将映射更改为静态,则会收到此链接器错误:

1>System.obj:错误 LNK2001: 未解析的外部符号“私有: 静态类 std::map,类 std::分配器>,int,结构 std::less,类 std::分配器> >,类 std::分配器,类 std::分配器>常量,整数> > > 系统::m_images” (?m_images@System@@0V?$map@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@HU?$less@V?$basic_string@ DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@2@V?$allocator@U?$pair@$$CBV?$basic_string@DU?$char_traits@D@ std@@V?$分配器@D@2@@std@@H@std@@@2@@std@@A)

你们中的聪明人能帮我吗?

I've been spoilt with C# coding the last few years and now I'm back onto C++ and finding that I'm having trouble with stuff that is supposed to be simple. I'm using a third party library for gamedev called DarkGDK (any commands which are prefixed with db), however DGDK isn't the problem.

Heres my code:

System.h

#pragma once

#include <string>
#include <map>
#include "DarkGDK.h"

using namespace std;

class System
{
public:
    System();
    ~System();
    void Initialize();

    static void LoadImage(string fileName, string id);
    static int GetImage(string id);

private:
    map<string, int> m_images;
};

System.cpp

#include "System.h"

System::System()
{
}

System::~System()
{
}

void System::Initialize()
{
    dbSetDisplayMode (1024, 640, 32);
    dbSetWindowTitle ("the Laboratory");
    dbSetWindowPosition(100, 10);

    dbSyncOn         ();
    dbSyncRate       (60);

    dbRandomize(dbTimer());
}

void System::LoadImage(string fileName, string id)
{
    int i = 1;

    while (dbImageExist(i))
    {
        i++;
    }

    dbLoadImage(const_cast<char*>(fileName.c_str()), i, 1);
    m_images[id] = i;
}

int System::GetImage(string id)
{
    return m_images[id];
}

The idea here is to have a map which maps strings against integer values, to access images with a string instead of hardcoded values. This class isn't done, so it doesn't handle anything like unloading images. I want to access the image methods without passing an instance of System, so I used static.

Now I get this error:

blahblah\system.cpp(39) : error C2677:
binary '[' : no global operator found
which takes type 'std::string' (or
there is no acceptable conversion)

If I change the map to static, I get this linker error:

1>System.obj : error LNK2001:
unresolved external symbol "private:
static class std::map,class
std::allocator >,int,struct
std::less,class
std::allocator > >,class
std::allocator,class
std::allocator > const ,int> > >
System::m_images"
(?m_images@System@@0V?$map@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@HU?$less@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@2@V?$allocator@U?$pair@$$CBV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@H@std@@@2@@std@@A)

Can any of you bright chaps help me out?

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

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

发布评论

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

评论(3

旧竹 2024-09-20 10:03:43

第一个是编译器错误,因为您无法从静态方法访问非静态数据成员。 this 指针不会隐式传递给静态方法,因此它们无法访问绑定到实例的数据成员。

在秒的情况下,请注意 static mapm_images; 只是变量的声明。您需要使用map定义静态成员变量System::m_images; 在源文件中。这将消除链接器错误。

The first one is a compiler error as you can not access the non-static data members from a static method. this pointer is not implictly passed to static methods hence they can not access data members bound to an instance.

In the seconds case, note that static map<string,int> m_images; is just a declaration of a variable. You need to define the static member variable using map<string, int> System::m_images; in the source file. This will get rid of the linker error.

凝望流年 2024-09-20 10:03:43

由于m_images是类的非静态成员,因此当您从静态成员函数访问它时,需要指定要使用其m_images成员的对象。如果只应该有一个由该类的所有对象共享的 m_images 对象,那么您也希望将其设为static

Since m_images is a non-static member of the class, when you access it from a static member function, you need to specify the object whose m_images member you want to use. If there's only supposed to be a single m_images object shared by all objects of that class, you want to make it static as well.

回心转意 2024-09-20 10:03:43

静态成员始终在程序中显式定义。所以它必须在 System.cpp 中以某种方式初始化。如果没有,您将收到未解决的外部错误。

以下是几个示例的链接

http://www. parashift.com/c++-faq-lite/ctors.html#faq-10.10

Static members are always explicitly defined in your program. So it has to be somehow initialised inside System.cpp. If not you get the unresolved external error.

Here's a link to a couple of examples

http://www.parashift.com/c++-faq-lite/ctors.html#faq-10.10

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