我运算符重载有问题??

weixin_38910147 2018-04-24 12:50:02
闲得无聊,简单写了一下String类;但是明明重载了运算符,却还是会报错。

class String
{
public://构造与析构
String();//无参构造函数
String(char* tmp);//带参构造函数初始化
String(String& tmp);//拷贝构造函数
~String();//析构
public://属性
int get_length();//返回字符串长度
bool is_empty();//字符串是否为空
public://查找
int index(String& tmp);//寻找子串首次出现的位置
String substring(int p, int len);//从字符串中第p个位置开始(包括第p个字符)获取长度为len的子串
public://替换
void replace_char(char tmp_1, char tmp_2);//字符串中将字符tmp_1,替换成tmp_2
void replace_string(String& tmp_1, String& tmp_2);//字符串中将子串tmp_1,替换成子串tmp_2
public://插入
void insert_char(char c, int p);//在字符串第p个位置上插入字符c
void insert_string(String& tmp, int p);//在字符串第p个位置上插入字符串tmp
public://删除
void delete_string(int p, int len);//在字符串中第p个位置(包括第p个字母)开始删除长度为len的子串
public://运算符重载
friend ostream& operator << (ostream& os, String& tmp);
friend istream& operator >> (istream& is, String& tmp);
friend bool operator == (String& tmp_1, String& tmp_2);
friend bool operator != (String& tmp_1, String& tmp_2);
friend bool operator < (String& tmp_1, String& tmp_2);
friend bool operator > (String& tmp_1, String& tmp_2);
friend String operator + (String& tmp_1, String& tmp_2);//字符串连接
friend String operator - (String& tmp_1, String& tmp_2);//在tmp_1中删除所有其包含的子串tmp_2
void operator = (String& tmp);//String类的赋值
void operator = (char* tmp);//字符串的赋值
private:
List<char> string;
int length;
};


String operator+(String & tmp_1, String & tmp_2)
{
String temp;
for (tmp_1.string.point = tmp_1.string.head; tmp_1.string.point; tmp_1.string.point = tmp_1.string.point->next)
{
temp.string.addToTail(tmp_1.string.point->info);
temp.length++;
}
for (tmp_2.string.point = tmp_2.string.head; tmp_2.string.point; tmp_2.string.point = tmp_2.string.point->next)
{
temp.string.addToTail(tmp_2.string.point->info);
temp.length++;
}
return temp;
}


void String::operator=(String & tmp)
{
while (!string.isEmpty())
{
string.deleteFromHead();
}
for (tmp.string.point = tmp.string.head; tmp.string.point != NULL; tmp.string.point = tmp.string.point->next)
{
string.addToTail(tmp.string.point->info);
}
length = tmp.length;
}


String::String(String & tmp)
{
for (tmp.string.point = tmp.string.head; tmp.string.point; tmp.string.point = tmp.string.point->next)
{
string.addToTail(tmp.string.point->info);
length++;
}
}


#include <iostream>
#include "String.h"

using namespace std;

int main()
{
String aa(const_cast<char*>("Kevin"));
String bb(const_cast<char*>("Salvatore"));
String cc(const_cast<char*>("Kevin"));
String dd;
dd = aa + bb + cc;
system("pause");
return 0;
}


为啥会报错啊?
有点不理解,求帮忙!!!

...全文
150 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
真相重于对错 2018-04-24
  • 打赏
  • 举报
回复
那么请问 自定义operator=算不算运算符重载
赵4老师 2018-04-24
  • 打赏
  • 举报
回复
引用 9 楼 hdt 的回复:
[quote=引用 8 楼 zhao4zhong1 的回复:] 运算符重载是语法糖。用有限的生命去品尝或品鉴无穷多种的语法糖,我认为不值当。
片面了吧? [/quote] 凡是可能导致调试难度变大的技术都是害人的技术。比如: 运算符重载 模板 自动垃圾收集 空值 空串 0开头八进制数 ……
赵4老师 2018-04-24
  • 打赏
  • 举报
回复
运算符重载是语法糖。用有限的生命去品尝或品鉴无穷多种的语法糖,我认为不值当。
真相重于对错 2018-04-24
  • 打赏
  • 举报
回复
引用 8 楼 zhao4zhong1 的回复:
运算符重载是语法糖。用有限的生命去品尝或品鉴无穷多种的语法糖,我认为不值当。
片面了吧?
weixin_38910147 2018-04-24
  • 打赏
  • 举报
回复
引用 6 楼 hdt 的回复:
[quote=引用 5 楼 weixin_38910147 的回复:] [quote=引用 3 楼 hdt 的回复:] void operator = (String& tmp);//String类的赋值 void operator = (char* tmp);//字符串的赋值 ---》 String& operator=(cons String& tmp) String& operator = (char* tmp); String& operator=(String&& tmp);//c++11以后
不是很明白,求详细解释! 是C++11后特性改了么?[/quote] 请百度 [/quote] 谢谢了,解决了 自己学的还是太落后了。 万分感谢!
weixin_38910147 2018-04-24
  • 打赏
  • 举报
回复
引用 3 楼 hdt 的回复:
void operator = (String& tmp);//String类的赋值 void operator = (char* tmp);//字符串的赋值 ---》 String& operator=(cons String& tmp) String& operator = (char* tmp); String& operator=(String&& tmp);//c++11以后
不是很明白,求详细解释! 是C++11后特性改了么?
真相重于对错 2018-04-24
  • 打赏
  • 举报
回复
引用 5 楼 weixin_38910147 的回复:
[quote=引用 3 楼 hdt 的回复:] void operator = (String& tmp);//String类的赋值 void operator = (char* tmp);//字符串的赋值 ---》 String& operator=(cons String& tmp) String& operator = (char* tmp); String& operator=(String&& tmp);//c++11以后
不是很明白,求详细解释! 是C++11后特性改了么?[/quote] 请百度
weixin_38910147 2018-04-24
  • 打赏
  • 举报
回复
引用 2 楼 paschen 的回复:
参数String& tmp_1, String& tmp_2改成:const String& tmp_1, const String& tmp_2 呢
不行,因为我用了我自己写的链表头文件,并且调用类内部的指针point(如下),所以不能用const。

template<typename Temp>
class List
{
public:
  Node<Temp> *head, *point, *tail;
  List();
  ~List();
  bool isEmpty();
  void addToHead(Temp tmp);
  void addToTail(Temp tmp);
  Temp deleteFromHead();
  Temp deleteFromTail();
  void deleteNode(Temp tmp);
  bool isInList(Temp tmp);
};
真相重于对错 2018-04-24
  • 打赏
  • 举报
回复
void operator = (String& tmp);//String类的赋值 void operator = (char* tmp);//字符串的赋值 ---》 String& operator=(cons String& tmp) String& operator = (char* tmp); String& operator=(String&& tmp);//c++11以后
paschen 版主 2018-04-24
  • 打赏
  • 举报
回复
参数String& tmp_1, String& tmp_2改成:const String& tmp_1, const String& tmp_2 呢
赵4老师 2018-04-24
  • 打赏
  • 举报
回复
《C++编程思想》

64,637

社区成员

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

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