typedef'ing STL wstring
Why is it when i do the following i get errors when relating to with wchar_t?
namespace Foo
{
typedef std::wstring String;
}
Now i declare all my strings as Foo::String through out the program, but when i ever attempt to create a new Foo::String from a wchar_t* i get an error, e.g.:
namespace Bar
{
static const wchar_t* COMMON_BAR = L"Hello";
}
int main()
{
Foo::String A(Bar::COMMON_BAR);
};
I get the following error from visual studio:
error C2440: '<function-style-cast>' : cannot convert from 'const wchar_t *' to 'Foo::String'
Whats wrong?
EDIT Sorry i ment to say Bar::COMMON_BAR :(
Also i am compiling on visual studio 2008. I am really frustrated by this.
EDIT#2
Sorry i couldn't respond to this sooner. The problems turns out to be a bigger issue than i wanted. I now noticed how much using std::wstring; 's there are floating around in the code.
This is at the absolute bottom of the global headerfile.
//开发者_运维知识库 WStrings are a waste
// Feb 2007
#define wstring string
Ugh. Thanks, sorry this is kinda a waste of space :(
The following code compiled and ran under llvm-gcc:
#include <string>
namespace Foo
{
typedef std::wstring String;
}
namespace Bar
{
static const wchar_t* COMMON_BAR = L"Hello";
}
int main()
{
Foo::String A(Bar::COMMON_BAR);
};
Notice how you accidentally had COMMON_DATA_PATH instead. I'm not sure which compiler you're using, but llvm-gcc gives the following error:
/tmp/webcompile/_1569_0.cc: In function 'int main()':
/tmp/webcompile/_1569_0.cc:14: error: 'COMMON_DATA_PATH' is not a member of 'Bar'
/tmp/webcompile/_1569_0.cc: At global scope:
/tmp/webcompile/_1569_0.cc:9: warning: 'Bar::COMMON_BAR' defined but not used
Try out the live demo here.
It compiles fine for me with the changes: Foo::String A(Bar::COMMON_BAR);
What is COMMON_DATA_PATH?
Why you have COMMON_DATA_PATH in constructor and COMMON_BAR in Foo? Is this misstype?
The code you posted compiles (with the addition of the #include <string>
) fine for me with the 32-bit VS 2008 compiler: Version 15.00.21022.08
So, I'm guessing you have something strange in your include path that's screwing up what std::wstring
is.
My include path has the following:
C:\Program Files\Microsoft Visual Studio 9.0\VC\ATLMFC\INCLUDE
C:\Program Files\Microsoft Visual Studio 9.0\VC\INCLUDE
C:\Program Files\Microsoft SDKs\Windows\v6.0A\include
C:\Program Files\Microsoft Visual Studio .NET 2003\SDK\v1.1\include\
Try to make yours equivalent and see if that improves things (actually, only the 2nd line should be necessary). If so, then you're on your way to adding things back until it breaks again to find out the culprit.
Another thing that might help is inside the IDE place your cursor on the wstring
symbol the right-click and select "Go to declaration". The IDE should open up the c:\Program Files\Microsoft Visual Studio 9.0\VC\include\xstring
header file on a declaration that looks something like:
typedef basic_string<wchar_t, char_traits<wchar_t>,
allocator<wchar_t> > wstring;
If it lands somewhere else, that's probably your problem.
You can do similar with the String
symbol in main()
and the IDE should jump to your typedef
.
精彩评论