求教CString类重载的+=运算符的问题

lilhope 2015-10-22 10:48:37
m_strline+=nChar;
其中m_strline是一个CString类对象,nChar是一个ASCII码,这条语句报错“more than one operator match these operand”。请问是什么原因,应该如何修改?
...全文
411 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
silence_101 2017-04-25
  • 打赏
  • 举报
回复
m_strline+=(TCHAR)nChar;
赵4老师 2015-10-22
  • 打赏
  • 举报
回复
楼主概念不清: ascii码不是一种数据类型。 电脑内存或文件内容只是一个一维二进制字节数组及其对应的二进制地址; 人脑才将电脑内存或文件内容中的这个一维二进制字节数组及其对应的二进制地址的某些部分看成是整数、有符号数/无符号数、浮点数、复数、英文字母、阿拉伯数字、中文/韩文/法文……字符/字符串、汇编指令、函数、函数参数、堆、栈、数组、指针、数组指针、指针数组、数组的数组、指针的指针、二维数组、字符点阵、字符笔画的坐标、黑白二值图片、灰度图片、彩色图片、录音、视频、指纹信息、身份证信息……
lilhope 2015-10-22
  • 打赏
  • 举报
回复
谢谢赵四老师的指导。由于cstring中分别重载了char与unsigned char类型的+=运算符,只要将ascii码显式转换为unsigned char型即可。
赵4老师 2015-10-22
  • 打赏
  • 举报
回复
ms-help://MS.VSCC.v90/MS.MSDNQTR.v90.chs/dv_vclib/html/5e352fc1-cdf9-4989-892e-c29a21f5c970.htm Shared Visual C++ Classes Reference CStringT::operator += Example See Also Send Feedback Concatenates characters to the end of the string. CStringT& operator+=( const CThisSimpleString& str ); template< bool bMFCDLL > CStringT& operator+=( const const CSimpleStringT<BaseType, bMFCDLL>& str ); template< int t_nSize > CStringT& operator+=( const CStaticString< XCHAR, t_nSize >& strSrc ); CStringT& operator+=( PCXSTR pszSrc ); CStringT& operator+=( PCYSTR pszSrc ); CStringT& operator+=( char ch ); CStringT& operator+=( unsigned char ch ); CStringT& operator+=( wchar_t ch ); CStringT& operator+=( const VARIANT& var ); Parameters str A reference to a CThisSimpleString object. bMFCDLL A boolean specifying whether the project is an MFC DLL or not. BaseType The string base type. var A variant object to concatenate to this string. ch An ANSI or Unicode character to concatenate with a string. pszSrc A pointer to the original string being concatenated. strSrc A CStringT to concatenate to this string. Remarks The operator accepts another CStringT object, a character pointer, or a single character. You should be aware that memory exceptions can occur whenever you use this concatenation operator because new storage can be allocated for characters added to this CStringT object. For information on CThisSimpleString, see the Remarks section of CStringT::CStringT. Note: Although it is possible to create CStringT instances that contain embedded null characters, we recommend against it. Calling methods and operators on CStringT objects that contain embedded null characters can produce unintended results. Example Visual C++ Copy Code // typedef CStringT<TCHAR, StrTraitATL<TCHAR, ChTraitsCRT<TCHAR>>> CAtlString; CAtlString s(_T("abc")); ASSERT((s += _T("def")) == _T("abcdef")); Requirements Header: cstringt.h See Also Concepts CStringT Class CStringT Members Send feedback on this topic to Microsoft.
赵4老师 2015-10-22
  • 打赏
  • 举报
回复
ms-help://MS.VSCC.v90/MS.MSDNQTR.v90.chs/dv_vclib/html/39569564-290e-4b6f-a829-2919556bee32.htm Shared Visual C++ Classes Reference CStringT::operator + Example See Also Send Feedback Concatenates two strings or a character and a string. friend CStringT operator+( const CStringT& str1, const CStringT& str2 ); friend CStringT operator+( const CStringT& str1, PCXSTR psz2 ); friend CStringT operator+( PCXSTR psz1 const CStringT& str2, ); friend CStringT operator+( char ch1 const CStringT& str2, ); friend CStringT operator+( const CStringT& str1, char ch2 ); friend CStringT operator+( const CStringT& str1, wchar_t ch2 ); friend CStringT operator+( wchar_t ch1 const CStringT& str2, ); Parameters ch1 An ANSI or Unicode character to concatenate with a string. ch2 An ANSI or Unicode character to concatenate with a string. str1 A CStringT to concatenate with a string or character. str2 A CStringT to concatenate with a string or character. psz1 A pointer to a null-terminated string to concatenate with a string or character. psz2 A pointer to a string to concatenate with a string or character. Remarks There are seven overload forms of the CStringT::operator+ function. The first version concatenates two existing CStringT objects. The next two concatenate a CStringT object and a null-terminated string. The next two concatenate a CStringT object and an ANSI character. The last two concatenate a CStringT object and a Unicode character. Note: Although it is possible to create CStringT instances that contain embedded null characters, we recommend against it. Calling methods and operators on CStringT objects that contain embedded null characters can produce unintended results. Example Visual C++ Copy Code // typedef CStringT<TCHAR, StrTraitATL<TCHAR, ChTraitsCRT<TCHAR>>> CAtlString; CAtlString s1(_T("dog ")), s2(_T(" awake")), s3; // Empty CAtlString objects s1= _T("The ") + s1; s3= s1 + _T('i'); s3= s3 + _T('s'); s3= s3 + s2; ASSERT(s3 == _T("The dog is awake")); Requirements Header: cstringt.h See Also Concepts CStringT Class CStringT Members Send feedback on this topic to Microsoft.
赵4老师 2015-10-22
  • 打赏
  • 举报
回复
查MSDN是Windows程序员必须掌握的技能之一。 ms-help://MS.VSCC.v90/MS.MSDNQTR.v90.chs/dv_vclib/html/41db66b2-9427-4bb3-845a-9b6869159a6c.htm MFC Library Reference Basic CString Operations See Also Send Feedback This article explains basic CString operations, including: Creating CString objects from standard C literal strings Accessing individual characters in a CString Concatenating two CString objects Comparing CString objects The CString class provides member functions and overloaded operators that duplicate and, in some cases, surpass the string services of the C run-time libraries (for example, strcat_s). Creating CString Objects from Standard C Literal Strings You can assign C-style literal strings to a CString just as you can assign one CString object to another: Assign the value of a C literal string to a CString object: Visual C++ Copy Code CString myString = _T("This is a test"); Assign the value of one CString to another CString object: Visual C++ Copy Code CString oldString = _T("This is a test"); CString newString = oldString; The contents of a CString object are copied when one CString object is assigned to another. Thus, the two strings do not share a reference to the actual characters that make up the string. For more information on using CString objects as values, see the article CString Semantics. Note: To write your application so that it can be compiled for Unicode or for ANSI, code literal strings using the _T macro. For more information, see the article Unicode and Multibyte Character Set (MBCS) Support. Accessing Individual Characters in a CString You can access individual characters within a CString object with the GetAt and SetAt member functions. You can also use the array element, or subscript, operator ( [ ] ) instead of GetAt to get individual characters (this is similar to accessing array elements by index, as in standard C-style strings). Index values for CString characters are zero based. Concatenating Two CString Objects To concatenate two CString objects, use the concatenation operators (+ or +=) as follows: Visual C++ Copy Code CString s1 = _T("This "); // Cascading concatenation s1 += _T("is a "); CString s2 = _T("test"); CString message = s1 + _T("big ") + s2; // Message contains "This is a big test". At least one argument to the concatenation operators (+ or +=) must be a CString object, but you can use a constant character string (such as "big") or a char (such as 'x') for the other argument. Comparing CString Objects The Compare member function and the == operator for CString are equivalent. Compare, operator==, and CompareNoCase are MBCS and Unicode aware; CompareNoCase is also case insensitive. The Collate member function of CString is locale sensitive and is often slower than Compare. Collate should be used only where it is necessary to abide by the sorting rules as specified by the current locale. The following table shows the available CString comparison functions and their equivalent Unicode/MBCS-portable functions in the C run-time library: CString function MBCS function Unicode function Compare _mbscmp wcscmp CompareNoCase _mbsicmp _wcsicmp Collate _mbscoll wcscoll The CString class overrides the relational operators (<, <=, >=, >, ==, and !=).You can compare two CStrings using these operators, as shown here: Visual C++ Copy Code CString s1(_T("Tom")); CString s2(_T("Jerry")); ASSERT(s2 < s1); See Also Concepts Strings (ATL/MFC) Send feedback on this topic to Microsoft.
lilhope 2015-10-22
  • 打赏
  • 举报
回复
引用 5 楼 zhao4zhong1 的回复:
楼主概念不清: ascii码不是一种数据类型。 电脑内存或文件内容只是一个一维二进制字节数组及其对应的二进制地址; 人脑才将电脑内存或文件内容中的这个一维二进制字节数组及其对应的二进制地址的某些部分看成是整数、有符号数/无符号数、浮点数、复数、英文字母、阿拉伯数字、中文/韩文/法文……字符/字符串、汇编指令、函数、函数参数、堆、栈、数组、指针、数组指针、指针数组、数组的数组、指针的指针、二维数组、字符点阵、字符笔画的坐标、黑白二值图片、灰度图片、彩色图片、录音、视频、指纹信息、身份证信息……
谢谢前辈。
paschen 版主 2015-10-22
  • 打赏
  • 举报
回复
m_strline+=CString(nChar)

65,176

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧