CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Jun 2012
    Posts
    3

    Trouble with the CString class

    Hi everyone,
    I am trying to use the CString class to concatenate some string but i am getting a strange compiler error. The code is as follows:

    void CTesterDlg::OnBnClickedButton1()
    {
    UpdateData(TRUE);
    m_Output=m_Hostname+"and";
    SetDlgItemText(IDC_EDIT2,m_Output);
    }

    Where m_Output and m_Hostname are CString variables.

    The compiler error is this:
    1>c:\documents and settings\administrator\my documents\visual studio 2005\projects\router monkey\router monkey\router monkeydlg.cpp(169) : error C2679: binary '+' : no operator found which takes a right-hand operand of type 'const char [2]' (or there is no acceptable conversion)
    1> e:\visual studio 8\vc\atlmfc\include\atlsimpstr.h(666): could be 'ATL::CSimpleStringT<BaseType> ATL::CSimpleStringT<BaseType>:perator +(const ATL::CSimpleStringT<BaseType> &,const ATL::CSimpleStringT<BaseType> &)' [found using argument-dependent lookup]

    Anyone know why this error is happening and how i can fix it ?

  2. #2
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,654

    Re: Trouble with the CString class

    I can't get from where the 'const char [2]' come from but that should work if you build for MBCS. Try this
    Code:
    m_Output=m_Hostname+_T("and");
    instead
    Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are, by
    definition, not smart enough to debug it.
    - Brian W. Kernighan

    To enhance your chance's of getting an answer be sure to read
    http://www.codeguru.com/forum/announ...nouncementid=6
    and http://www.codeguru.com/forum/showthread.php?t=366302 before posting

    Refresh your memory on formatting tags here
    http://www.codeguru.com/forum/misc.php?do=bbcode

    Get your free MS compiler here
    https://visualstudio.microsoft.com/vs

  3. #3
    Join Date
    Jun 2012
    Posts
    3

    Re: Trouble with the CString class

    Thank S_M_A that worked !

    Another silly question. Why doesn't the newline operator work i am trying to output a string to the an edit box but the \n doesn't seem to work:

    m_Output=_T("testing \n")+m_Hostname;

  4. #4
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,654

    Re: Trouble with the CString class

    I haven't done any windows programming for quite a long time now but if I don't remember wrong you use \r in an edit box. Also make sure it's a multi line enabled edit box.
    Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are, by
    definition, not smart enough to debug it.
    - Brian W. Kernighan

    To enhance your chance's of getting an answer be sure to read
    http://www.codeguru.com/forum/announ...nouncementid=6
    and http://www.codeguru.com/forum/showthread.php?t=366302 before posting

    Refresh your memory on formatting tags here
    http://www.codeguru.com/forum/misc.php?do=bbcode

    Get your free MS compiler here
    https://visualstudio.microsoft.com/vs

  5. #5
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    8,234

    Re: Trouble with the CString class

    Beside the fact that your edit control must be multi-line (have ES_MULTILINE style set) you have to use "\r\n" (CRLF, "carriage return" and "line feed" / "new line") for line delimiter.
    Ovidiu
    "When in Rome, do as Romans do."
    My latest articles: https://codexpertro.wordpress.com/

  6. #6
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,654

    Re: Trouble with the CString class

    Should have looked that up instead of relying on my bad memory...
    Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are, by
    definition, not smart enough to debug it.
    - Brian W. Kernighan

    To enhance your chance's of getting an answer be sure to read
    http://www.codeguru.com/forum/announ...nouncementid=6
    and http://www.codeguru.com/forum/showthread.php?t=366302 before posting

    Refresh your memory on formatting tags here
    http://www.codeguru.com/forum/misc.php?do=bbcode

    Get your free MS compiler here
    https://visualstudio.microsoft.com/vs

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured