views:

120

answers:

2

Hi, i want to concatenate 2 strings in c++, i can't use char*.

I tried the following but doesn't work:

#define url L"http://domain.com"
wstring s1 = url;
wstring s2 = L"/page.html";
wstring s = s1 + s2;
LPOLESTR o = OLESTR(s);

I need a string with s1 and s2 concatenated. Any info or website that explain more about this ? Thanks.

+2  A: 

You're missing the L to make s2's assignment work.

wstring s2 = L"/page.html";
tloach
just changed it, but it gives me: 'error C2065: 'Ls' : undeclared identifier' on line 'LPOLESTR o = OLESTR(s);'
BHOdevelopper
+3  A: 

OLESTR("s") is the same as L"s" (and OLESTR(s) is Ls), which is obviously not what you want.
Use this:

#define url L"http://domain.com"
wstring s1 = url;
wstring s2 = L"/page.html";
wstring s = s1 + s2;
LPCOLESTR o = s.c_str();

This gives you a LPCOLESTR (ie. a const LPOLESTR). If you really need it to be non-const, you can copy it to a new string:

...
wstring s = s1 + s2;
LPOLESTR o = new wchar_t[s.length() + 1];
wcscpy(o, s.c_str()); //wide-string equivalent of strcpy is wcscpy
//Don't forget to delete o!

Or, to avoid wstring's altogether (not recommended; it would be better to convert your application to using wstring's everywhere than to use LPOLESTR's):

#define url L"http://domain.com"
LPCOLESTR s1 = url;
LPCOLESTR s2 = L"/page.html";
LPOLESTR s = new wchar_t[wcslen(s1) + wcslen(s2) + 1];
wcscpy(s, s1); //wide-string equivalent of strcpy is wcscpy
wcscat(s, s2); //wide-string equivalent of strcat is wcscat
//Don't forget to delete s!
BlueRaja - Danny Pflughoeft