0

Compiling the following code...

#define UNICODE
#include<wchar.h>
#include<windows.h>
#include<string>

int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,
                   LPSTR lpCmdLine,int nCmdShow)
{
    LPWSTR str1=L"vishal ";
    LPWSTR str2=L"nechwani";
    LPWSTR str3=str1 + str2;
    MessageBox(NULL,str3,str3,MB_OK);
    return 0;
}

...produces this error:

error==>error: invalid operands of types 'LPWSTR {aka wchar_t*}' and 'LPWSTR {aka wchar_t*}' to binary 'operator+'

Why can't I concatenate two strings like this?

zett42
  • 25,437
  • 3
  • 35
  • 72
  • 5
    `LPWSTR` is a pointer to an array of wide characters. It is not a class with a + overload, so you cannot concatenate `LPWSTR`s with +. Consider using `wstring` instead. If you must suffer through c-style strings, `wcscat`, but do not forget to pre-allocate storage. – user4581301 Jan 02 '18 at 19:56
  • 1
    @user4581301 You should turn that into an answer. – Ron Jan 02 '18 at 20:00
  • @Ron good point. – user4581301 Jan 02 '18 at 20:05
  • 3
    `LPWSTR str3=str1 + str2;` tries to add two pointer values. That is neither _concatenating strings_ or even valid syntax. Use either `std::wstring` or some appropriate `strcat()` variant working with _wide char_ strings. – user0042 Jan 02 '18 at 20:07
  • @user0042 Also a good point. Mind if I steal part of that comment? – user4581301 Jan 02 '18 at 20:09

1 Answers1

8

LPWSTR is a pointer to an array of wide characters. It is not a class with a + overload, so you cannot concatenate LPWSTRs with +. Consider using wstring instead.

#define UNICODE
#include<windows.h>
#include<string>
int main()
{
    std::wstring str1(L"vishal ");
    std::wstring str2(L"nechwani");
    std::wstring str3 = str1 + str2;
    MessageBox(NULL, str3.c_str(), str3.c_str(), MB_OK);
    return 0;
}

If you must suffer through c-style strings, use wcscat, but do not forget to pre-allocate storage for str3.

Edit: Doing this the Stupid Way

This is the stupid way because look at all the extra work you have to do:

#define UNICODE
#include<cwchar>
#include<windows.h>

int main()
{
    LPCWSTR str1=L"vishal "; // note LPCWSTR. L"vishal " provides a constant array
                             // and it should be assigned to a constant pointer
    LPCWSTR str2=L"nechwani";

    // find out how much space we need
    size_t len = wcslen(str1) + // length string 1
                 wcslen(str2) + // length string 2
                 1; // null terminator. Can't have a c-style string without one

    LPWSTR str3 = new wchar_t[len]; // allocate space for concatenated string
                                    // Note you could use a std::unique_ptr here, 
                                    // but for smurf's sake just use a wstring instead
    str3[0] = L'\0'; // null terminate string

    //add string 1 and string 2 to to string 3
    wcscat(str3,str1);
    wcscat(str3,str2);

    MessageBox(NULL,str3,str3,MB_OK);
    delete[] str3; // release storage allocated to str3
    return 0;
}

No shame in being confused by this mess. It is a mess.

wcsncat may not be the right tool to use here. To properly display the concatenated string you must either size the buffer so that it is too big to truncate or allocate a buffer large enough to contain the string. I've chosen to allocate a sufficiently large buffer. Also note that wcsncat can still overrun the end of your buffer placing the null terminator, so the count parameter must be no more than one less than the size of the buffer.

wstring does all of this crap for you and adds on a number of other useful operations free of charge. Not using a string without a good reason to avoid string is stupid. Don't be stupid.

Community
  • 1
  • 1
user4581301
  • 33,082
  • 7
  • 33
  • 54