problems adding 2 CString's together
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: problems adding 2 CString's together

  1. #1
    Join Date
    Apr 2001
    Posts
    1,029

    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!

  2. #2
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    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");
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  3. #3
    ovidiucucu's Avatar
    ovidiucucu is offline Moderator/Reviewer Power Poster
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    8,021

    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
    Ovidiu Cucu
    "When in Rome, do as Romans do."
    Visit: Microsoft Virtual Academy
    Follow: https://twitter.com/#!/ovidiucucu
    My blog: http://codexpert.ro/blog/author/ovidiu-cucu/

  4. #4
    Arjay's Avatar
    Arjay is offline Moderator / MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    11,217

    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
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center