CString + UINT Error:有多个运算符"+="与这些操作数匹配

在OnChar中,参数UINT nChar

 

有一个CString str,现在执行

str += nChar报错:Error:有多个运算符"+="与这些操作数匹配

 

解决办法:把UINT强制转换为TCHAR

 

目前我是这么做的,不知道有没有什么问题

好的,这是一道关于C++中字符串运算符重载的编程作业。要求实现字符串的拼接运算,即将一个字符串S2添加到另一个字符串S1的末尾,实现S1+=S2的操作运算。 以下是友元函数实现的代码示例: ``` #include <iostream> #include <cstring> using namespace std; class String { char* str; int size; public: String() { size = 0; str = new char[size+1]; str[0] = '\0'; } String(char* s) { size = strlen(s); str = new char[size+1]; strcpy(str, s); } friend String operator+=(String &s1, String &s2); void display() { cout << str << endl; } }; String operator+=(String &s1, String &s2) { char* temp = new char[s1.size + s2.size + 1]; strcpy(temp, s1.str); strcat(temp, s2.str); s1.size += s2.size; delete [] s1.str; s1.str = temp; return s1; } int main() { String s1("Hello"); String s2(" World!"); s1 += s2; s1.display(); return 0; } ``` 以下是成员函数实现的代码示例: ``` #include <iostream> #include <cstring> using namespace std; class String { char* str; int size; public: String() { size = 0; str = new char[size+1]; str[0] = '\0'; } String(char* s) { size = strlen(s); str = new char[size+1]; strcpy(str, s); } String operator+=(String &s) { char* temp = new char[size + s.size + 1]; strcpy(temp, str); strcat(temp, s.str); size += s.size; delete [] str; str = temp; return *this; } void display() { cout << str << endl; } }; int main() { String s1("Hello"); String s2(" World!"); s1 += s2; s1.display(); return 0; } ``` 以上两个代码示例都可以实现字符串的拼接运算,使用了不同的实现方式。友元函数的方式可以直接访问类的私有成员变量,而成员函数实现则需要使用this指针来访问类的成员变量。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值