用string类派生出一个MyString类,为什么有等号的地方都错了呢?

时间:2022-09-07 19:21:09
#include<string.h>
#include<stdlib.h>
#include<string>
#include<iostream>
using namespace std;
class MyString:public string
{
public:
MyString(){}
MyString(char*s):string(s){}
MyString(const MyString& n):string(n){}
string& operator=(const MyString other);
};
string& MyString::operator=(const MyString other)
{
(string)(*this)=(string&)(other);
return *this;
}
int CompareString(const void *e1,const void *e2)
{
MyString*s1=(MyString*)e1;
MyString*s2=(MyString*)e2;
if(*s1<*s2)
return -1;
if(*s1==*s2)
return 0;
if(*s1>*s2)
return 1;
return 0;
}
int main()
{
MyString s1("abcd-"),s2,s3("efgh-"),s4(s1);
MyString SArray[4]={"big","me","about","take"};
cout<<"1."<<s1<<s2<<s3<<s4<<endl;
s4=s3;
s3=s1+s3;
cout<<"2."<<s1<<endl;
cout<<"3."<<s2<<endl;
cout<<"4."<<s3<<endl;
cout<<"5."<<s4<<endl;
cout<<"6."<<s1[2]<<endl;

s2=s1;
s1="ijkl-";
s1[2]='A';
cout<<"7."<<s2<<endl;
cout<<"8."<<s1<<endl;
s1+="mnop";
cout<<"9."<<s1<<endl;
s4="qrst-"+s2;
cout<<"10."<<s4<<endl;
s1=s2+s4+" uvw "+"xyz";
cout<<"11."<<s1<<endl;
qsort(SArray,4,sizeof(MyString),CompareString);
for(int i=0;i<4;i++)
cout<<SArray[i]<<endl;
}

这是我们的作业,我只完成了一部分,还望各位大神赐教!

24 个解决方案

#1


忘了说问题了,main函数中有等号的都错了,应当怎么改呢?

#2


(string)(*this)=(string&)(other);
改成
(string&)(*this)=(string&)(other);
试试

#3


什么叫错? 编译错还是运行错

#4


引用 2 楼 adlay 的回复:
(string)(*this)=(string&)(other);
改成
(string&)(*this)=(string&)(other);
试试

还是不行······

#5


Quote: 引用 3 楼 derekrose 的回复:

什么叫错? 编译错还是运行错[/quote
编译错误,eclipse提示:
no match for 'operator=' in 's1 = std::operator+(const std::basic_string<_CharT, _Traits, _Alloc>&, const _CharT*) [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>](((const char*)"xyz"))' MyString2.cpp /MyString2 line 59 C/C++ Problem

#6


老师让你们去继承stl的std::string了?

#7


还让你们重写operator=?
引用 6 楼 mougaidong 的回复:
老师让你们去继承stl的std::string了?

#8


引用 7 楼 mougaidong 的回复:
还让你们重写operator=?
Quote: 引用 6 楼 mougaidong 的回复:

老师让你们去继承stl的std::string了?
要求我们继承string然后再实现一些其它的功能,可我现在连string原有的功能都实现不了

#9


引用 7 楼 mougaidong 的回复:
还让你们重写operator=?
Quote: 引用 6 楼 mougaidong 的回复:

老师让你们去继承stl的std::string了?
不是继承的时候operator=必须要重写吗?

#10


引用 5 楼 u013159250 的回复:
Quote: 引用 3 楼 derekrose 的回复:

什么叫错? 编译错还是运行错[/quote
编译错误,eclipse提示:
no match for 'operator=' in 's1 = std::operator+(const std::basic_string<_CharT, _Traits, _Alloc>&, const _CharT*) [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>](((const char*)"xyz"))' MyString2.cpp /MyString2 line 59 C/C++ Problem

是不是因为operator+=没有定义?

#11


引用 10 楼 ws_yqr 的回复:
Quote: 引用 5 楼 u013159250 的回复:

Quote: 引用 3 楼 derekrose 的回复:

什么叫错? 编译错还是运行错[/quote
编译错误,eclipse提示:
no match for 'operator=' in 's1 = std::operator+(const std::basic_string<_CharT, _Traits, _Alloc>&, const _CharT*) [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>](((const char*)"xyz"))' MyString2.cpp /MyString2 line 59 C/C++ Problem

是不是因为operator+=没有定义?

++

#12


operator=必须重新定义,基类的operator=是属于基类的,和派生类没有关系的

#13


引用 12 楼 worldy 的回复:
operator=必须重新定义,基类的operator=是属于基类的,和派生类没有关系的

不实现的话,应该会生成默认的吧

#14


引用 13 楼 ken_scott 的回复:
Quote: 引用 12 楼 worldy 的回复:

operator=必须重新定义,基类的operator=是属于基类的,和派生类没有关系的

不实现的话,应该会生成默认的吧


那+=有默认的吗?

#15


引用 14 楼 worldy 的回复:
Quote: 引用 13 楼 ken_scott 的回复:

Quote: 引用 12 楼 worldy 的回复:

operator=必须重新定义,基类的operator=是属于基类的,和派生类没有关系的

不实现的话,应该会生成默认的吧


那+=有默认的吗?
应该有吧,因为那里没有报错

#16


引用 12 楼 worldy 的回复:
operator=必须重新定义,基类的operator=是属于基类的,和派生类没有关系的
重新定义过了啊,只是好像不对,s3=s1+s3这句,提示右值不匹配,可两个string类对象加起来的返回值是什么呢?

#17


别继承string,记得他的析构函数不是虚函数。

#18


string& MyString::operator=(const MyString other)
改成
MyString& MyString::operator=(const MyString& other)

(string)(*this)=(string)(other);
改成
this->string ::operator=(other);


#19


(string)(*this)=(string)(other);
改成
string ::operator=(other);

#20


编译不通过,是因为,你的operator =返回的是string,但是你没有实现string到Mystring的构造函数,因此,编译器就不知道如何用string得到Mystring。我加了下面一条语句,就通过编译了,你可以试试,有个warning,返回局部变量值,小心!!!
MyString(string s):string(s){}

#21


谢谢大家,问题解决了,结贴!

#22


引用 5 楼 u013159250 的回复:
Quote: 引用 3 楼 derekrose 的回复:

什么叫错? 编译错还是运行错[/quote
编译错误,eclipse提示:
no match for 'operator=' in 's1 = std::operator+(const std::basic_string<_CharT, _Traits, _Alloc>&, const _CharT*) [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>](((const char*)"xyz"))' MyString2.cpp /MyString2 line 59 C/C++ Problem


53行operator+没有定义,在派生类里实现operator+

#23


作为一个STL的容器,string类的设计和其它容器基本一样。这些STL都不鼓励被继承。

#24


引用 14 楼 worldy 的回复:
Quote: 引用 13 楼 ken_scott 的回复:

Quote: 引用 12 楼 worldy 的回复:

operator=必须重新定义,基类的operator=是属于基类的,和派生类没有关系的

不实现的话,应该会生成默认的吧


那+=有默认的吗?

这倒没有

#1


忘了说问题了,main函数中有等号的都错了,应当怎么改呢?

#2


(string)(*this)=(string&)(other);
改成
(string&)(*this)=(string&)(other);
试试

#3


什么叫错? 编译错还是运行错

#4


引用 2 楼 adlay 的回复:
(string)(*this)=(string&)(other);
改成
(string&)(*this)=(string&)(other);
试试

还是不行······

#5


Quote: 引用 3 楼 derekrose 的回复:

什么叫错? 编译错还是运行错[/quote
编译错误,eclipse提示:
no match for 'operator=' in 's1 = std::operator+(const std::basic_string<_CharT, _Traits, _Alloc>&, const _CharT*) [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>](((const char*)"xyz"))' MyString2.cpp /MyString2 line 59 C/C++ Problem

#6


老师让你们去继承stl的std::string了?

#7


还让你们重写operator=?
引用 6 楼 mougaidong 的回复:
老师让你们去继承stl的std::string了?

#8


引用 7 楼 mougaidong 的回复:
还让你们重写operator=?
Quote: 引用 6 楼 mougaidong 的回复:

老师让你们去继承stl的std::string了?
要求我们继承string然后再实现一些其它的功能,可我现在连string原有的功能都实现不了

#9


引用 7 楼 mougaidong 的回复:
还让你们重写operator=?
Quote: 引用 6 楼 mougaidong 的回复:

老师让你们去继承stl的std::string了?
不是继承的时候operator=必须要重写吗?

#10


引用 5 楼 u013159250 的回复:
Quote: 引用 3 楼 derekrose 的回复:

什么叫错? 编译错还是运行错[/quote
编译错误,eclipse提示:
no match for 'operator=' in 's1 = std::operator+(const std::basic_string<_CharT, _Traits, _Alloc>&, const _CharT*) [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>](((const char*)"xyz"))' MyString2.cpp /MyString2 line 59 C/C++ Problem

是不是因为operator+=没有定义?

#11


引用 10 楼 ws_yqr 的回复:
Quote: 引用 5 楼 u013159250 的回复:

Quote: 引用 3 楼 derekrose 的回复:

什么叫错? 编译错还是运行错[/quote
编译错误,eclipse提示:
no match for 'operator=' in 's1 = std::operator+(const std::basic_string<_CharT, _Traits, _Alloc>&, const _CharT*) [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>](((const char*)"xyz"))' MyString2.cpp /MyString2 line 59 C/C++ Problem

是不是因为operator+=没有定义?

++

#12


operator=必须重新定义,基类的operator=是属于基类的,和派生类没有关系的

#13


引用 12 楼 worldy 的回复:
operator=必须重新定义,基类的operator=是属于基类的,和派生类没有关系的

不实现的话,应该会生成默认的吧

#14


引用 13 楼 ken_scott 的回复:
Quote: 引用 12 楼 worldy 的回复:

operator=必须重新定义,基类的operator=是属于基类的,和派生类没有关系的

不实现的话,应该会生成默认的吧


那+=有默认的吗?

#15


引用 14 楼 worldy 的回复:
Quote: 引用 13 楼 ken_scott 的回复:

Quote: 引用 12 楼 worldy 的回复:

operator=必须重新定义,基类的operator=是属于基类的,和派生类没有关系的

不实现的话,应该会生成默认的吧


那+=有默认的吗?
应该有吧,因为那里没有报错

#16


引用 12 楼 worldy 的回复:
operator=必须重新定义,基类的operator=是属于基类的,和派生类没有关系的
重新定义过了啊,只是好像不对,s3=s1+s3这句,提示右值不匹配,可两个string类对象加起来的返回值是什么呢?

#17


别继承string,记得他的析构函数不是虚函数。

#18


string& MyString::operator=(const MyString other)
改成
MyString& MyString::operator=(const MyString& other)

(string)(*this)=(string)(other);
改成
this->string ::operator=(other);


#19


(string)(*this)=(string)(other);
改成
string ::operator=(other);

#20


编译不通过,是因为,你的operator =返回的是string,但是你没有实现string到Mystring的构造函数,因此,编译器就不知道如何用string得到Mystring。我加了下面一条语句,就通过编译了,你可以试试,有个warning,返回局部变量值,小心!!!
MyString(string s):string(s){}

#21


谢谢大家,问题解决了,结贴!

#22


引用 5 楼 u013159250 的回复:
Quote: 引用 3 楼 derekrose 的回复:

什么叫错? 编译错还是运行错[/quote
编译错误,eclipse提示:
no match for 'operator=' in 's1 = std::operator+(const std::basic_string<_CharT, _Traits, _Alloc>&, const _CharT*) [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>](((const char*)"xyz"))' MyString2.cpp /MyString2 line 59 C/C++ Problem


53行operator+没有定义,在派生类里实现operator+

#23


作为一个STL的容器,string类的设计和其它容器基本一样。这些STL都不鼓励被继承。

#24


引用 14 楼 worldy 的回复:
Quote: 引用 13 楼 ken_scott 的回复:

Quote: 引用 12 楼 worldy 的回复:

operator=必须重新定义,基类的operator=是属于基类的,和派生类没有关系的

不实现的话,应该会生成默认的吧


那+=有默认的吗?

这倒没有