C++ std::将字符串映射到指针

C++ std::map string to pointer

本文关键字:指针 映射 std C++ 字符串      更新时间:2023-10-16

我目前正在将C++与 Direct3D 一起使用,并正在尝试更改我的资产存储。

现在我的资产存储为"矢量纹理列表",并使用枚举定义获得。

我想通过使用 STL map 类使类更加开放。但是,我以前从未使用过这个类,我遇到了我不理解的问题。

我刚刚将所有内容都进行了简单的测试,目前有以下内容:

#include <d3d9.h>
#include <d3dx9.h>
#include <map>
#include <string>
#pragma comment (lib, "d3d9.lib") 
#pragma comment (lib, "d3dx9.lib") 
using namespace std;

class Assets
{
private:
typedef std::map<string, IDirect3DTexture9*> TexMap;
TexMap textureList;
public:
IDirect3DTexture9* LoadTexture(IDirect3DDevice9* pd3dDevice, LPCWSTR file, string key)
{     
    //load a texture from file
    IDirect3DTexture9*      tex;

    //D3DXCreateTextureFromFile(pd3dDevice, file, &tex);
    D3DXCreateTextureFromFileEx(pd3dDevice, file, 512, 512, 0, 0, D3DFMT_UNKNOWN, D3DPOOL_MANAGED, D3DX_DEFAULT, D3DX_DEFAULT, 0xFF000000, NULL, NULL, &tex);

    //store the loaded texture to a vector array
    textureList.insert(TexMap::value_type(key, tex));
    return S_OK;
}
}

当我尝试运行它时,我收到"调试断言失败"错误,表达式为"映射/设置迭代器不兼容"

我只是觉得我已经在代码中将其简化为罐头,但查看类似示例仍然看不到错误。

我还运行了代码:

#include <d3d9.h>
#include <d3dx9.h>
#include <map>
#include <string>
#pragma comment (lib, "d3d9.lib") 
#pragma comment (lib, "d3dx9.lib") 
using namespace std;
class Assets
{
private:
typedef std::map<int, int> TexMap;
TexMap textureList;
public:
IDirect3DTexture9* LoadTexture(IDirect3DDevice9* pd3dDevice, LPCWSTR file, string key)
    {     
    //load a texture from file
    IDirect3DTexture9*      tex;

    //D3DXCreateTextureFromFile(pd3dDevice, file, &tex);
    D3DXCreateTextureFromFileEx(pd3dDevice, file, 512, 512, 0, 0, D3DFMT_UNKNOWN, D3DPOOL_MANAGED, D3DX_DEFAULT, D3DX_DEFAULT, 0xFF000000, NULL, NULL, &tex);

    //store the loaded texture to a vector array
    //textureList.push_back(tex);
    textureList.insert(TexMap::value_type(3, 4));
    return S_OK;
}
}

只是因为它只是使用整数,我仍然得到同样的错误。

这个错误实际上可能是非常基本的——D3DXCreateTextureFromFileEx想要类型 IDirect3DTexture9* 作为它的最后一个参数,但你给它类型 IDirect3DTexture9** . 也就是说,改变

D3DXCreateTextureFromFileEx(..., &tex);

D3DXCreateTextureFromFileEx(..., tex);