views:

125

answers:

5

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
      }
+3  A: 

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
ok i gave you where is the error is happening
Ramiz Toma
@Ramiz: The code above doesn't change the answer. The compiler simply doesn't know what CA2W is.
Billy ONeal
ok but how should i know where ca2w can be found
Ramiz Toma
i checked out the msdn and they said to include atlconv.h but it still can't find it
Ramiz Toma
@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.
AndreyT
i know what it does from the tutorial that i read and i copied their code
Ramiz Toma
@Ramiz Toma: So you basically copied-and-pasted code without bothering to understand what it does?
In silico
i do know what it does
Ramiz Toma
the function changes it from LPSTR to LPCWSTR
Ramiz Toma
@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.
AndreyT
@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
well i have atlconv.h defined
Ramiz Toma
Error 5 error C3861: 'CA2W': identifier not found c:\users\owner\documents\visual studio 2010\projects\monopoly\monopoly\monopoly.cpp 170
Ramiz Toma
A: 

CA2W is defined in <atlconv.h>.

Greg Hewgill
i did that but it didn't work
Ramiz Toma
@Ramiz Toma: Care to tell us how it didn't work? Did you get a compiler error? What was the error(s), if any?
In silico
Error 5 error C3861: 'CA2W': identifier not found c:\users\owner\documents\visual studio 2010\projects\monopoly\monopoly\monopoly.cpp 170
Ramiz Toma
A: 

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
still same problem
Ramiz Toma
+3  A: 

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>.

In silico
i have them already and i still get the error
Ramiz Toma
@Ramiz Toma: Seriously? Did you get the same error? What exact error message did you get? The code snippet above compiles just fine on my machine.
In silico
do you think i would waste my time over this. i have these two headers but i still get the same error i gave you
Ramiz Toma
@Ramiz Toma: Really? I took the time to launch a dummy project in Visual Studio, research the documentation, create what should be a compilable code snippet, despite the fact there's zero incentive for me to try to help you, and all you have to say is that I think I would waste *your* time? All I asked was for some information about your code/environment, because the code snippet I have above works for me, so obviously something is different about what I have and what you have. Have you tried compiling the above code snippet by itself?
In silico
i didn't mean you were wasting my time i was saying that i would waste your time and my time if it was working. i created a win32 projects and i added the headers you told me about than i added the sinnpet you gave me but still ca2w is not found
Ramiz Toma
@Ramiz Toma: Have you tried explicitly qualifying the name with the ATL namespace? `CA2W` lives in the `ATL` namespace. I've edited my code snippet.
In silico
ok i update it i got this error error C2039: 'CA2W' : is not a member of 'ATL'
Ramiz Toma
can you tell me what version of windows sdk do you have? i have 7.0 and the 2003
Ramiz Toma
I have 7.0, but it shouldn't matter since the ATL header files are located in the Visual Studio installation directory, not in the Windows SDK.
In silico
well when i compile the headers that you gave me i have to include the location of the headers in windows sdk
Ramiz Toma
Yes, because the ATL headers depend on `<windows.h>`, but the ATL headers themselves are located in the VS installation directory.
In silico
Come to think of it, what ATL version do you have? That might be the reason why my compilation result was different.
In silico
how can i find the version im using?
Ramiz Toma
How about you tell me what version of Visual Studio you're using?
In silico
2010 express edition
Ramiz Toma
@Ramiz Toma: That's your problem! The express editions don't actually have the complete ATL or MFC library. I used a non-express edition of Visual Studio, which is why it worked on my machine.
In silico
well is there something i can do about it?
Ramiz Toma
@Ramiz Toma: Yes. You can use the `MultiByteToWideChar()` function found in the Windows SDK.
In silico
man thanks alot :)
Ramiz Toma
@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
@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
A: 

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".

Ann Catherine Jose