第四章:运算符的重载的一系列问题(课堂作业)
2015-04-26 18:40
375 查看
#include<istream> using namespace std; #include<string.h> class String { public: String(); virtual ~String(); String(int n); String(const String &s2); String(const char *str); String & operator = (const String &s1); friend bool operator >(String &string1, String &string2); friend bool operator <(String &string1, String &string2); friend bool operator == (String &string1, String &string2); friend bool operator <= (String &string1, String &string2); friend bool operator >= (String &string1, String &string2); friend ostream& operator << (ostream& , String &); private: char *s; }; String::String()//初始化,默认长度为100 { s = new char[100]; s[0] = '\0'; } String::String(int n)//构造一个大小为n的数组 { s = new char ; s[0] = '\0'; } String::~String()//析构 { if(s) { delete s; } s = NULL; } String::String(const String &s2)//拷贝构造函数 { this->s = new char [strlen[s2.s]+1] ; //ERROR :C:\Users\Administrator\Desktop\I can do it\fsada.cpp(69) : error C2107: illegal index, indirection not allowed //ERROR: C:\Users\Administrator\Desktop\I can do it\fsad//error //C2440: 'initializing' : //cannot convert from 'char *' to 'int' s[0] = '\0'; strcpy(s, s2.s); } String::String(const char *str) { s = new char[strlen[str]+1];//同上 = =。 s[0] = '\0'; } String& String :: operator = (const String &s1)//赋值构造函数 { if(this == &s1) { return *this; } else { delete []s1; //ERROR :No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called //ERRORE:fatal error C1903: unable to recover from previous error(s); stopping compilation s1 = new char [strlen[s1.s]+1]; return *this; } } bool operator > (String &string1, String &string2) { if(strcmp(string1.s , string2.s) > 0) return true; else return false; } bool operator < (String &string1, String &string2) { if(strcmp(string1.s , string2.s) < 0) return true; else return false; } bool operator == (String &string1, String &string2) { if(strcmp(string1.s , string2.s) == 0) return true; else return false; } bool operator <= (String &string1, String &string2) { if(strcmp(string1.s , string2.s) > 0) return false; else return true; } bool operator >= (String &string1, String &string2) { if(strcmp(string1.s , string2.s) < 0) return false; else return true; } ostream& operator << (ostream& output , String & s3) { output << s3.s << endl; return output; }没办法找出错误的原因。无法运行 = =。
相关文章推荐
- 第四章 重载课堂作业
- 课堂作业-购书问题
- C++重载自增运算符的效率问题
- 关于C#相等运算符(==)重载的问题
- VC6.0中有关输出运算符<<的重载问题(备忘)
- 重载课堂作业
- C++编程点滴4:二元运算符重载问题
- operator运算符的重载问题
- 第四章课堂作业
- 5-26课堂作业——组员投票Alpha版存在的问题
- 第四章函数作业题,函数的重载
- 科锐课堂笔记:2017/4/12 单例模式与运算符重载
- 软件工程课堂作业——购书问题
- 第八周任务二:time类中运算符的重载问题
- 课堂作业之买书问题
- 运算符的重载问题
- python 运算符 重载可使用
- C++中不能重载的运算符
- 作业3 应用分支与循环结构解决问题。
- 分数运算符的重载