int operator==(string a, string b){}在operator处报错
时间: 2024-05-16 20:16:39 浏览: 4
这个错误通常是由于在类外定义运算符重载函数时,没有指定该函数所属的类名导致的。请尝试将该函数定义修改为:
```
bool operator==(const string& a, const string& b) {}
```
其中,`string` 表示该函数所属的类名,`bool` 表示运算符重载函数的返回类型。此外,在比较字符串时建议使用 `const string&` 类型的引用,可以避免不必要的拷贝操作,提高代码效率。
相关问题
完成下列的 String 类,并在主函数 main( )中测试它。 class String { public: String(const char *str = NULL); // constructor String(const String &other); // copy constructor ~ String(void); // destructor String & operate =(char *str); String & operate =(const String &other);// 重载=运算符 int operator==(String &other); // 重载==运算符 int operator==(char *str); private: char *m_data; // used for storing the string int length; };
以下是 String 类的实现以及在主函数 main() 中的测试。
```cpp
#include <iostream>
#include <cstring>
using namespace std;
class String {
public:
String(const char *str = NULL);
String(const String& other);
~String(void);
String& operator=(char *str);
String& operator=(const String& other);
int operator==(String& other);
int operator==(char *str);
private:
char *m_data;
int length;
};
String::String(const char* str) {
if (str != NULL) {
length = strlen(str);
m_data = new char[length + 1];
strcpy(m_data, str);
}
else {
length = 0;
m_data = new char[1];
*m_data = '\0';
}
}
String::String(const String& other) {
length = other.length;
m_data = new char[length + 1];
strcpy(m_data, other.m_data);
}
String::~String(void) {
delete[] m_data;
}
String& String::operator=(char *str) {
if (m_data == str) {
return *this;
}
if (str != NULL) {
length = strlen(str);
char *temp = new char[length + 1];
strcpy(temp, str);
delete[] m_data;
m_data = temp;
}
else {
length = 0;
delete[] m_data;
m_data = new char[1];
*m_data = '\0';
}
return *this;
}
String& String::operator=(const String& other) {
if (m_data == other.m_data) {
return *this;
}
length = other.length;
char *temp = new char[length + 1];
strcpy(temp, other.m_data);
delete[] m_data;
m_data = temp;
return *this;
}
int String::operator==(String& other) {
return strcmp(m_data, other.m_data) == 0;
}
int String::operator==(char *str) {
return strcmp(m_data, str) == 0;
}
int main() {
String s1("hello");
String s2(s1);
String s3("world");
String s4;
s4 = s1;
cout << s1.operator==("hello") << endl; // 1
cout << s1.operator==(s2) << endl; // 1
cout << s1.operator==(s3) << endl; // 0
cout << s1.operator==(s4) << endl; // 1
return 0;
}
```
请实现operator+以进行如下操作: string1 = string2 + string3
好的,我理解你的要求是将两个字符串用等号连接起来赋值给另外一个字符串。我可以为你实现一个名为`operator=`的函数来达成这个目的:
```c++
#include <iostream>
#include <string>
using namespace std;
int main() {
string string1, string2 = "hello", string3 = "world";
string1 = string2 + " " + string3;
cout << string1 << endl; // 输出 "hello world"
return 0;
}
```
这个程序中,我们首先定义了三个字符串变量`string1`、`string2`和`string3`。然后,我们使用`+`运算符将`string2`和`string3`连接起来,中间用空格隔开。最后,我们将连接后的字符串赋值给`string1`。
如果你需要在你的程序中重载`operator=`函数来实现这个操作,你可以这样写:
```c++
#include <iostream>
#include <string>
using namespace std;
class MyString {
private:
string str;
public:
MyString() {}
MyString(const string& s) : str(s) {}
MyString operator=(const MyString& rhs) {
str = rhs.str;
return *this;
}
MyString operator=(const string& s) {
str = s;
return *this;
}
friend MyString operator+(const MyString& lhs, const MyString& rhs) {
return MyString(lhs.str + rhs.str);
}
friend MyString operator+(const MyString& lhs, const string& rhs) {
return MyString(lhs.str + rhs);
}
friend MyString operator+(const string& lhs, const MyString& rhs) {
return MyString(lhs + rhs.str);
}
friend ostream& operator<<(ostream& os, const MyString& ms) {
os << ms.str;
return os;
}
};
int main() {
MyString string1, string2 = "hello", string3 = "world";
string1 = string2 + " " + string3;
cout << string1 << endl; // 输出 "hello world"
return 0;
}
```
这个程序中,我们定义了一个名为`MyString`的类来表示字符串,重载了`operator=`函数和`operator+`函数。在`operator=`函数中,我们将右操作数的字符串赋值给左操作数的字符串,并返回左操作数的引用。在`operator+`函数中,我们将两个字符串连接起来,并返回一个新的`MyString`对象。最后,我们在`main`函数中使用这些运算符来实现你要求的操作,并输出结果。