c ++'CA2W':找不到标识符

Ram*_*lol 1 c++ winapi visual-c++

为什么我得到'CA2W':找不到标识符

for(DWORD i = 0; i < numMaterials; i++)    // for each material...
    {
        material[i] = tempMaterials[i].MatD3D;    // get the material info
        material[i].Ambient = material[i].Diffuse;    // make ambient the same as diffuse
        USES_CONVERSION;    // allows certain string conversions
        // if there is a texture to load, load it
        D3DXCreateTextureFromFile(d3ddev,
                                            CA2W(tempMaterials[i].pTextureFilename),
                                            &texture[i]);
        texture[i] = NULL;    // if there is no texture, set the texture to NULL
      }
Run Code Online (Sandbox Code Playgroud)

In *_*ico 8

编辑:

OP刚刚告诉我,Visual Studio 2010 Express用于编译代码.这可以解释为什么CA2W找不到,因为Express版本不包括整个ATL/MFC库.因此,我原来的答案与OP无关.

故事的寓意:确保在提出这些问题时确切地提到你所处的环境.

CA2W您可以使用MultiByteToWideChar()- 而不是使用- 此函数实际上用于CA2W转换字符串,因此您将获得基本相同的结果.

这是MultiByteToWideChar()另一个Stack溢出问题的答案中的用法示例.它是从转换std::stringLPCWSTR,但它基本上是相同的过程.

原始答案:

根据ATL和MFC字符串转换宏的文档,您必须包括:

#include <atlbase.h>
#include <atlconv.h>

int main()
{ 
    // Explicitly qualified the CA2W identifier
    ATL::CA2W("Hello World!"); // Test to see if this compiles
} 
Run Code Online (Sandbox Code Playgroud)

我想,为什么这个代码片段之前没有工作的原因是因为你#define倒是_ATL_NO_AUTOMATIC_NAMESPACE在什么地方#include <atlbase.h>.

  • @Ramiz Toma:这是Stack溢出时`MultiByteToWideChar()`的一个例子:http://stackoverflow.com/questions/27220/how-to-convert-stdstring-to-lpcwstr-in-c-unicode (2认同)