problems adding 2 CString's together
Hello,
Below is the code which gives me the error:
Code:
CString temp1, path;
GetCurrentDirectory(1000, temp1.GetBufferSetLength(1000));
path = temp1 + "\\subdir\\test1.jpg";
error:
error C2679: binary '+' : no operator found which takes a right-hand
operand of type 'const char [18]' (or there is no acceptable conversion)
How can I fix this?
Thanks!
Re: problems adding 2 CString's together
Is this an ANSI build? Otherwise, you might need to wrap the string in the _T() macro.
But anyway, this works for sure:
Code:
path = temp1 + CString("\\subdir\\test1.jpg");
Re: problems adding 2 CString's together
Beside what cilu already stated.
- Use _T() macro to allow compiling both UNICODE and ANSI project configurations.
- The code
Code:
path = temp1 + CString("\\subdir\\test1.jpg");
compiles (even in UNICODE build) because CString has a constructor which takes 'const unsigned char*' as parameter;
however
Code:
path = temp1 + _T("\\subdir\\test1.jpg");
does fewer operations, so it's better. - After using CString::GetBufferSetLength, call CString::ReleaseBuffer before other operations; otherwise, you may get unexpected results.
- It's not necessary to use temporary CString objects as long as CString has an operator +=.
- Avoid hard-coded constants; in this case you can use _MAX_PATH instead of "magic" number 1000.
Concluding, I woud like to suggest something like:
Code:
CString strPath;
const DWORD dwLength = _MAX_PATH + 1;
GetCurrentDirectory(dwLength, strPath.GetBufferSetLength(dwLength));
strPath.ReleaseBuffer();
strPath += _T("\\subdir\\test1.jpg");
Re: problems adding 2 CString's together
Btw, if you want to type less, use GetBuffer( ) instead of GetBufferSetLength( ).