2012-08-08 52 views
0

我目前使用C++与Direct3D,并试图改变我的资产存储。C++ std :: map字符串指针

现在我的资产被存储为“vector textureList”并且使用枚举定义获得。

我想通过使用STL地图类来使类更加开放。不过,我从来没有使用过这门课,而且我遇到了我只是不明白的问题。

我刚才做任何事情都很裸机测试,目前有以下几种:

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

当我试图运行此我得到一个“调试断言失败”错误与表达“图/套迭代器不兼容“

我只是觉得我已经在代码中尽可能简化了代码,并且仍然无法从类似的例子中看到错误。正是如此它只是使用int,我仍然得到同样的错误

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

我也运行的代码。

+0

什么是调用代码?那里没有“主”。我的猜测是你在某个地方使迭代器无效。 – netcoder 2012-08-08 19:33:43

+0

是的,目前主要功能加载场景管理器,场景管理器创建资产类别。我会将主要和任何相关部分的代码添加到第一篇文章中。迭代器如何失效? – user1539405 2012-08-08 20:08:52

+0

请参阅[迭代器失效规则](http://stackoverflow.com/questions/6438086/iterator-invalidation-rules)。 – netcoder 2012-08-08 20:11:45

回答

0

实际上这个错误可能是非常基本的 - D3DXCreateTextureFromFileEx想要输入IDirect3DTexture9*作为其最后一个参数,但是您输入了IDirect3DTexture9**。也就是说,改变

D3DXCreateTextureFromFileEx(..., &tex); 

D3DXCreateTextureFromFileEx(..., tex); 
+0

实际上这个函数是在IDirect3DTexture9 **之后(或者更具体地说是“LPDIRECT3DTEXTUR9 * ppTexture”)。除此之外,当我使用基于矢量的存储时,纹理加载正常。谢谢你。 – user1539405 2012-08-08 20:06:47

+0

考虑到这个函数调用中没有任何东西使用'map',我怀疑这是问题所在。 – netcoder 2012-08-08 20:12:39