English 中文(简体)
typedef ing STL wstring
原标题:
  • 时间:2009-11-13 04:07:57
  •  标签:
  • c++
  • typedef

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 FilesMicrosoft Visual Studio 9.0VCATLMFCINCLUDE
C:Program FilesMicrosoft Visual Studio 9.0VCINCLUDE
C:Program FilesMicrosoft SDKsWindowsv6.0Ainclude
C:Program FilesMicrosoft Visual Studio .NET 2003SDKv1.1include

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 FilesMicrosoft Visual Studio 9.0VCincludexstring 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.





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

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

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签