-
March 13th, 2011, 11:55 AM
#1
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!
-
March 14th, 2011, 01:39 AM
#2
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");
-
March 14th, 2011, 03:00 AM
#3
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");
Last edited by ovidiucucu; March 14th, 2011 at 03:03 AM.
Reason: typos
-
March 14th, 2011, 02:13 PM
#4
Re: problems adding 2 CString's together
Btw, if you want to type less, use GetBuffer( ) instead of GetBufferSetLength( ).
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|