C++运算符重载代码编译报C2679错误,不修改main函数如何修复
C++运算符重载代码修复方案
问题根因
编译报错的直接原因是MyString类的相等运算符重载不符合调用要求:List::find方法传入的是const T&类型参数,但MyString::operator==的入参是非const左值引用,且没有对当前对象加const修饰,无法匹配(T, const T)的调用签名。除此之外代码还存在多处隐含问题,会导致运行异常、内存泄漏等问题,所有修改均未改动main函数,符合要求。
需修改的点
- 修复
MyString类的operator==实现:原代码参数为非const引用,且strcmp返回值逻辑倒置(strcmp相等时返回0,直接返回会导致相等判断结果为false) - 给
List类重载[]运算符:原代码多处调用list[i]但未实现对应重载,需同时实现普通版本和const版本 - 给
List类实现拷贝构造函数:main函数中存在拷贝构造调用List<MyString> s2List(sList);,默认浅拷贝会导致同一块内存被两个对象持有,析构时重复释放 - 修复
List类的赋值运算符operator=实现:原实现没有清空原有内容,count未重置,且遍历用了总容量list.size而非有效元素数list.count,缺少数组容量越界判断 - 给
List类的getSize方法加const修饰:方便const对象调用,兼容ostream重载的使用场景 - 给
Complex类的operator==加const修饰:避免const对象调用时报错,符合运算符重载最佳实践 - 补充
<cstring>头文件引入:用到的strlen、strcmp函数定义在该头文件中
完整修改后可运行代码
#include <iostream> #include <cstring> using namespace std; template <class T> class List { T* items{ nullptr }; int count{ 0 }; int size{ 0 }; public: List<T>(const int _size) { items = new T[_size]; size = _size; } // 新增拷贝构造函数 List(const List<T>& list) { size = list.size; count = list.count; items = new T[size]; for (int i = 0; i < count; i++) { items[i] = list.items[i]; } } void add(const T& value) { cout << value << " is added" << endl; items[count++] = value; } List& operator =(const List& list) { if (this == &list) return *this; count = 0; if (size < list.count) { delete[] items; size = list.size; items = new T[size]; } for (int i = 0; i < list.count; i++) { items[count++] = list.items[i]; } return *this; } // 加const修饰 int getSize() const { return count; } // 新增[]运算符重载 T& operator[](int index) { return items[index]; } const T& operator[](int index) const { return items[index]; } bool operator == (const List<T>& t) { if (count != t.count) return false; for (auto i = 0; i < count; i++) if (items[i] != t[i]) return false; return true; } bool find(const T& t) const { for (auto i = 0; i < count; i++) if (items[i] == t) return true; return false; } // 补充析构函数避免内存泄漏 ~List() { delete[] items; } }; template <class T> ostream& operator << (ostream& os, List<T>& list) { for (auto i = 0; i < list.getSize(); i++) os << list[i] << endl; return os; } class Complex { private: float real, imaginary; public: Complex() { real = 0; imaginary = 0; } Complex(float _real, float _imaginary) { real = _real; imaginary = _imaginary; } // 加const修饰 bool operator == (const Complex& complex) const { return ((complex.imaginary == imaginary) && (complex.real == real)); } friend ostream& operator <<(ostream& ostream, const Complex& complex); }; ostream& operator <<(ostream& ostream, const Complex& complex) { ostream << complex.real << "," << complex.imaginary; return ostream; } class MyString { private: char* str; public: MyString() { str = nullptr; } MyString(const char* string) { str = new char[strlen(string) + 1]; for (unsigned int i = 0; i < strlen(string); i++) str[i] = string[i]; str[strlen(string)] = '\0'; } // 补充拷贝构造避免浅拷贝问题 MyString(const MyString& another) { str = new char[strlen(another.str) + 1]; strcpy(str, another.str); } MyString& operator = (const MyString& another) { if (this == &another) return *this; delete[] str; str = new char[strlen(another.str) + 1]; strcpy(str, another.str); return *this; } // 修复operator== bool operator == (const MyString& string) const { if (!str && !string.str) return true; if (!str || !string.str) return false; return strcmp(string.str, str) == 0; } friend ostream& operator <<(ostream& ostream, const MyString& string); // 补充析构函数 ~MyString() { delete[] str; } }; ostream& operator <<(ostream& ostream, const MyString& string) { ostream << string.str; return ostream; } int main() { List<Complex> cList(100); cList.add(Complex(0, 0)); cList.add(Complex(1, 1)); cout << cList; cout << std::boolalpha; cout << cList.find(Complex(1, 1)) << endl; // true cout << cList.find(Complex(1, 0)) << endl; // false List<MyString> sList(200); sList.add("abc"); sList.add("def"); cout << sList; cout << sList.find("def") << endl; // true cout << sList.find("Aef") << endl; // false List<MyString> s2List(sList); s2List.add("ghi"); cout << s2List; List<MyString> s3List(10); s3List = s2List; s3List.add("jkl"); cout << s3List; }
内容的提问来源于stack exchange,提问作者iamianpark
相关产品推荐
相关产品推荐

