c++ - 如何使用好的运算符重载?
问题描述
#include <iostream>
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;
}
void add(const T& value) {
cout << value << " is added" << endl;
items[count++] = value;
}
List& operator =(const List& list) {
for (int i = 0; i < list.size; i++) {
items[count++] = list.items[i];
}
return *this;
}
int getSize()
{
return count;
}
bool operator == (const List<T>& t) {
for (auto i = 0; i < count; i++)
if (items[i] != t[i])
return false;
return true;
}
bool find(const T& t)
{
for (auto i = 0; i < count; i++)
if (items[i] == t)
return true;
return false;
}
};
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;
}
bool operator == (const Complex& complex) {
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& operator = (const MyString& another) {
delete str;
str = new char[strlen(another.str) + 1];
for (unsigned int i = 0; i < strlen(another.str); i++)
str[i] = another.str[i];
str[strlen(another.str)] = '\0';
return *this;
}
bool operator == (MyString& string) {
return strcmp(string.str, str);
}
friend ostream& operator <<(ostream& ostream, const MyString& string);
};
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;
}
嗨,我正在学习 C++,今天我有一个问题。我试图更正我的代码,但它仍然说
Build started...
1>------ Build started: Project: study, Configuration: Debug Win32 ------
1>study.cpp
1>study.cpp(43,1): error C2679: binary '==': no operator found which takes a right-hand operand of type 'const T' (or there is no acceptable conversion)
1> with
1> [
1> T=MyString
1> ]
1>study.cpp(105,7): message : could be 'bool MyString::operator ==(MyString &)'
1>study.cpp(43,1): message : while trying to match the argument list '(T, const T)'
1> with
1> [
1> T=MyString
1> ]
1>study.cpp(41): message : while compiling class template member function 'bool List<MyString>::find(const T &)'
1> with
1> [
1> T=MyString
1> ]
1>study.cpp(132): message : see reference to function template instantiation 'bool List<MyString>::find(const T &)' being compiled
1> with
1> [
1> T=MyString
1> ]
1>study.cpp(128): message : see reference to class template instantiation 'List<MyString>' being compiled
1>Done building project "study.vcxproj" -- FAILED.
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
首先,我无法纠正我的主要功能。这是我代码的一种骨架,我的教授说不要更改主要功能,只需修复你的课程。感谢您阅读我的问题!!我需要你的帮助。
解决方案
你错过了包含<cstring>
(strcmp
只是偶然你没有得到错误)。
当一个方法不修改其通过引用传递的参数时,该引用应该是const
. 改变:
bool operator == (MyString& string) {
return strcmp(string.str, str);
}
至:
bool operator == (const MyString& string) {
return strcmp(string.str, str);
}
只有这样才能进行从字符串文字到的转换MyString
。
修复这些后,我剩下:
<source>:55:19: error: no match for 'operator[]' (operand types are 'List<Complex>' and 'int')
55 | os << list[i] << endl;
| ~~~~^
那是因为List<T>
根本没有operator[]
。