This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center




已关闭10年。




为什么我得到“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
      }

最佳答案

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

故事的寓意:在问这类问题时,请务必准确提及您所处的环境。

除了使用CA2W之外,还可以使用 MultiByteToWideChar() -此功能实际上是CA2W用来转换字符串的功能,因此您将获得基本相同的结果。

Here's a usage example of MultiByteToWideChar() in an answer to another Stack overflow question。这是从std::stringLPCWSTR的转换,但是基本上是相同的过程。

原始答案:根据the documentation for ATL and MFC String Conversion Macros,您必须包括:

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

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

我认为此代码段之前不起作用的原因是因为您在#define之前的某个地方_ATL_NO_AUTOMATIC_NAMESPACE'd #include <atlbase.h>

10-04 13:15