?帮忙看看简单的重载运算符有何问题

gl615 2008-05-11 03:05:17
一道作业题。
但刚写完第四个函数就出错了,很郁闷...。
请好心人指点。
先谢谢了。。
===================================================================================================
#include <iostream>
using namespace std;

class MyString
{
public:
int Getlength(); // 1
MyString(); // 2
MyString(char* str); // 3
MyString(MyString& another); // 4
MyString operator+(const MyString& another); // 5
MyString& operator=(const MyString& another); // 6
bool operator==(const MyString& another)const; // 7
char& operator[](int pos); // 8
operator char*(); // 9

protected:
friend ostream& operator<<(ostream& o,MyString& p); // 10
char* m_data;
int length;
};

int MyString::Getlength()
{
return length;
}

MyString::MyString()
{
m_data=new char[1];
m_data[0]='\0';
length=0;
}

MyString::MyString(char* str)
{
length=strlen(str);
m_data=new char[length+1];
int i;
for(i=0;i<length;i++)
{
m_data[i]=str[i];
}
m_data[i]='\0';
}

MyString::MyString(MyString& another)
{
length=another.Getlength();
m_data=new char[length+1];
int i;
for(i=0;i<length;i++)
{
m_data[i]=another[i];
}
m_data[i]='\0';
}

MyString MyString::operator+(const MyString& another)
{
int t = length + another.Getlength(); ----------------------------------// 62 error 1
MyString result(t); ----------------------------------------------------// 63 error 2
int i,j;
for(i=0;i<length;i++)
{
result[i]=m_data[i];
}
for(i=length,j=0;i<t,j<another.Getlength();i++,j++) -------------------// 69 error 3
{
result[i]=another[j];-------------------------------------------// 71 error 4
}
result[t]='\0';
return result;
}

void main()
{
MyString a="hehe";

}
================================================================================================
Compiling...
aaa.cpp
C:\Program Files\Microsoft Visual Studio\MyProjects\aaa\aaa.cpp(62) : error C2662: 'Getlength' : cannot convert 'this' pointer from 'const class MyString' to 'class MyString &'
Conversion loses qualifiers
C:\Program Files\Microsoft Visual Studio\MyProjects\aaa\aaa.cpp(63) : error C2664: '__thiscall MyString::MyString(char *)' : cannot convert parameter 1 from 'int' to 'char *'
Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
C:\Program Files\Microsoft Visual Studio\MyProjects\aaa\aaa.cpp(69) : error C2662: 'Getlength' : cannot convert 'this' pointer from 'const class MyString' to 'class MyString &'
Conversion loses qualifiers
C:\Program Files\Microsoft Visual Studio\MyProjects\aaa\aaa.cpp(71) : error C2678: binary '[' : no operator defined which takes a left-hand operand of type 'const class MyString' (or there is no acceptable conversion)
Error executing cl.exe.

aaa.exe - 4 error(s), 0 warning(s)
-----------------------------------------------------------------------------------------------------
我用的是VC++6.0编译器。
...全文
51 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
gl615 2008-05-11
  • 打赏
  • 举报
回复
非常感谢,

只怪这个题目函数的声明顺序不合适,搞得有点郁闷。。
hastings 2008-05-11
  • 打赏
  • 举报
回复
71的错误见2楼回复:
const char& operator[](int pos)const;
char& operator[](int pos);

63的问题是与构造函数不匹配.
gl615 2008-05-11
  • 打赏
  • 举报
回复
先谢谢hastings了,
但为什么没加 const在Getlength()后就错了啊?

还有就是现在加了之后,63与71的错误仍然在. ?

两个小小疑问请回答。
hastings 2008-05-11
  • 打赏
  • 举报
回复
operator[]声明两个版本:
const版本和非const版本.
hastings 2008-05-11
  • 打赏
  • 举报
回复
把Getlength()声明成const的

64,680

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧