1

why do i get 'CA2W': identifier not found

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
      }
GManNickG
  • 494,350
  • 52
  • 494
  • 543
Ramilol
  • 3,394
  • 11
  • 52
  • 84
  • 6
    Like I said a bit ago, you really need to start over and learn *just* C++. Learn really good, solid, clean, basic C++, then move upwards, *then* try game programming. Game programming isn't trivial, and you can't really get anywhere if you're trying to learn idiomatic C++ at the same time. Really, I think we'd all like to see you get better, but we really can't do much more. At some point you have to take it upon yourself to take the time to really learn the fundamentals. – GManNickG Oct 10 '10 at 05:13

5 Answers5

12

Edit:

The OP has just told me that Visual Studio 2010 Express was used to compile the code. That would explain why CA2W couldn't be found, because the Express editions do not include the entire ATL/MFC library. Therefore, my original answer is irrelevant to the OP.

The moral of the story: make sure to mention exactly what environment you're on when asking these kinds of questions.

Instead of using CA2W, you can use MultiByteToWideChar() - this function is what's actually being used by CA2W to convert the string, so you'll get basically the same result.

Here's a usage example of MultiByteToWideChar() in an answer to another Stack overflow question. It's for a conversion from std::string to LPCWSTR, but it's basically the same process.

Original answer:

According to the documentation for ATL and MFC String Conversion Macros you must include:
#include <atlbase.h>
#include <atlconv.h>

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

I think the reason why this code snippet didn't work before was because you #define'd _ATL_NO_AUTOMATIC_NAMESPACE somewhere before #include <atlbase.h>.

Community
  • 1
  • 1
In silico
  • 51,091
  • 10
  • 150
  • 143
  • @Ramiz Toma: Yes. You can use the `MultiByteToWideChar()` function found in the Windows SDK. – In silico Oct 10 '10 at 06:26
  • 1
    @Ramiz Toma: You can find usage examples of `MultiByteToWideChar()` right here on Stack overflow, as well as on other sites. If you have problems with using the function, I highly suggest asking another question, so I don't clutter my answer any more than it is. – In silico Oct 10 '10 at 06:38
  • 2
    @Ramiz Toma: Here's an example of `MultiByteToWideChar()` on Stack overflow: http://stackoverflow.com/questions/27220/how-to-convert-stdstring-to-lpcwstr-in-c-unicode – In silico Oct 10 '10 at 06:44
  • @Insilico, you said in the other answer that including `atlconv.h` should be enough. Why do you also include `atlbase.h`? For some reason, in my case I just need to include the latter - and if I only include the former, the code does not compile. Which is weird because the [MSDN says](https://learn.microsoft.com/en-us/cpp/atl/reference/ca2wex-class?view=vs-2019) it's in the `atlconv` header. – Marc.2377 Sep 13 '19 at 23:18
3

Show us your code, otherwise we are powerless to help you.

The error you are getting means: "What the heck is CA2W???"

It means you are using the identifier CA2W, but it hasn't been declared as anything.

Alexander Rafferty
  • 6,134
  • 4
  • 33
  • 55
  • ok i gave you where is the error is happening – Ramilol Oct 10 '10 at 04:19
  • @Ramiz: The code above doesn't change the answer. The compiler simply doesn't know what CA2W is. – Billy ONeal Oct 10 '10 at 04:20
  • ok but how should i know where ca2w can be found – Ramilol Oct 10 '10 at 04:21
  • i checked out the msdn and they said to include atlconv.h but it still can't find it – Ramilol Oct 10 '10 at 04:23
  • 3
    @Ramiz Toma: If you don't know what `CA2W` is, why are you using it in the code? This is no different from if you typed some gibberish in the code, like `jcgdhfgdh`, and then started asking why the compiler doesn't know what it is. – AnT stands with Russia Oct 10 '10 at 04:23
  • i know what it does from the tutorial that i read and i copied their code – Ramilol Oct 10 '10 at 04:24
  • 3
    @Ramiz Toma: So you basically copied-and-pasted code without bothering to understand what it does? – In silico Oct 10 '10 at 04:40
  • the function changes it from LPSTR to LPCWSTR – Ramilol Oct 10 '10 at 04:50
  • @Ramiz Toma: Well, if you know what it is, then you should know where it is supposed to come from: library or such. A library, for example, will supply you with a header file that will contain the declaration of `CA2W`. You have to `#include` that header in your code. – AnT stands with Russia Oct 10 '10 at 04:52
  • 1
    @Ramiz Toma: It is in fact not a function. It's a typedef to a `CA2WEX` class. Basically you're constructing a temporary `CA2WEX` that performs the conversion. Including `atlconv.h` should be sufficient to solve the problem since it's defined there. The fact that it still doesn't work (as you mentioned in another answer) means we need more information (like say, compiler errors). – In silico Oct 10 '10 at 04:54
  • well i have atlconv.h defined – Ramilol Oct 10 '10 at 04:58
  • Error 5 error C3861: 'CA2W': identifier not found c:\users\owner\documents\visual studio 2010\projects\monopoly\monopoly\monopoly.cpp 170 – Ramilol Oct 10 '10 at 05:07
3

So try this - Create a Win32 console project, then replace the given code with this below that compiles fine for me. If this does not compile for you, I'm stumped - perhaps re-install Visual Studio? Seems like multiple people can compile this properly, so what might be wrong with your Visual Studio installation?

// testconv.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <atlconv.h>
#include <atlbase.h> 
#include <atlstr.h>

int _tmain(int argc, _TCHAR* argv[])
{
   USES_CONVERSION;
   WCHAR *pChar = CA2W(NULL);
    return 0;
}
Jeff
  • 1,969
  • 3
  • 21
  • 35
1

CA2W is defined in <atlconv.h>.

Greg Hewgill
  • 951,095
  • 183
  • 1,149
  • 1,285
0

Check if the path to the ATL header files atlconv.h and atlbase.h is visible to Visual Studio

In your monopoly.cpp file, right-click on the line #include <atlconv.h> and select Open document atlconv.h. If the header file opens properly, then it means there is no problem with the location. Instead, if it throws an error, then it means VStudio cannot locate the file and that's the reason why it says 'identifier not found'.

In the error message, VStudio displays the path to the folders where it looked for the header file. Check if the list contains a path similar to "C:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\ce\atlmfc\include", where the ATL header files are present. If it is missing, include the path (use the path appropriate to your OS and the version of VStudio that you are using) as follows: Visual Studio: Tools->Options->Projects and Solutions->VC++ Directories-> "Show directories for Include files".