Click to See Complete Forum and Search --> : CString to wstring
aaudette
Dec 29th, 2000, 12:37 PM
Anybody know of a way to convert a CString into a wstring?
parksie
Dec 29th, 2000, 01:02 PM
If UNICODE is defined, then you can use:
wstring str = LPCTSTR(myCString);
If not, then you'd have to use an ordinary string.
aaudette
Dec 29th, 2000, 01:26 PM
WHen I try that it returns:
error C2440: 'initializing' : cannot convert from 'const char *' to 'class std::basic_string<unsigned short,struct std::char_traits<unsigned short>,class std::allocator<unsigned short> >'
No constructor could take the source type, or constructor overload resolution was ambiguous
I have defined Unicode like this:
#ifndef UNICODE
#define UNICODE
#endif
parksie
Dec 29th, 2000, 01:28 PM
It's giving a char error because CString doesn't think it's using Unicode. If you assigned it to a string, then it would be fine.
aaudette
Dec 29th, 2000, 01:39 PM
im not sure I follow. What am I assigning a string?
Do you mean:
string str;
CString Cstr;
wstring wstr;
wstr=Cstr=str;
??
parksie
Dec 29th, 2000, 01:42 PM
#ifdef _UNICODE
#define String wstring
#else
#define String string
#endif
String mystring;
CString myCString = "Hello";
mystring = LPCTSTR(myCString);
aaudette
Dec 29th, 2000, 02:01 PM
It fails to recognize String as a datatype. Heres what I think you're getting at:
#include <windows.h>
#include <vector.h>
#include <lm.h>
#include <stdio.h>
#ifdef _UNICODE
#define String wstring
#else
#define String string
#endif
main()
{
String mystring;
CString myCString = "Hello";
mystring = LPCTSTR(myCString);
return 0;
}
And heres the build status:
--------------------Configuration: temp - Win32 Debug--------------------
Compiling...
temp.cpp
C:\temp.cpp(15) : error C2065: 'string' : undeclared identifier
C:\temp.cpp(15) : error C2146: syntax error : missing ';' before identifier 'mystring'
C:\temp.cpp(15) : error C2065: 'mystring' : undeclared identifier
C:\temp.cpp(16) : error C2065: 'CString' : undeclared identifier
C:\temp.cpp(16) : error C2146: syntax error : missing ';' before identifier 'myCString'
C:\temp.cpp(16) : error C2065: 'myCString' : undeclared identifier
C:\temp.cpp(16) : error C2440: '=' : cannot convert from 'char [6]' to 'int'
This conversion requires a reinterpret_cast, a C-style cast or function-style cast
C:\temp.cpp(18) : error C2440: '=' : cannot convert from 'const char *' to 'int'
This conversion requires a reinterpret_cast, a C-style cast or function-style cast
Error executing cl.exe.
temp.exe - 8 error(s), 0 warning(s)
parksie
Dec 29th, 2000, 05:38 PM
AAAAARRRRGGGGHHHH!!! ;)
Hehehe...you need to tell it you're using the string class... ;)
#include <string>
using std::string;
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.