找回密码
 注册
搜索
热搜: 回贴
  • 前程无忧官网首页 有什么好的平台可以
  • 最新的销售平台 互联网营销的平台有哪
  • 制作网页的基本流程 网页制作和网页设
  • 【帝国CMS】输出带序号的列表(数字排
  • 网站建设公司 三一,中联,极东泵车的
  • 织梦 建站 织梦网站模版后台怎么更改
  • 云服务官网 哪些网站有免费的简历模板
  • 如何建网站要什么条件 建网站要用什么
  • 吉林市移动公司电话 吉林省退休人员网
  • 设计类毕业论文 网站设计与实现毕业论
查看: 304|回复: 5

一个关于流操作符重载的问题,

[复制链接]
发表于 2009-11-3 03:34:27 | 显示全部楼层 |阅读模式 IP:江苏扬州
#include<iostream>
#include<string.h>
using namespace std;
class String;
class String{
friend istream& operator>>(istream& is,String& s1);
friend ostream& operator<<(ostream& os,const String& s2);
public:
String():str(0),len(0){};
String(char *_str);
String(const String &s);
~String();
String operator=(const String& s);
String operator=(char *_str);
String operator+(const String &s);
String operator+(char *_str);
int operator>(const String& );
int find(String &);
int length(){
return len;
}
char get_str(){
return *str;
}
private:
char* str;
int len;
};
String::String(char *strc){
len=strlen(strc)+1;
str=new char[len+1];
strcpy(str,strc);
}
String::String(const String &s):len(s.len){
str=new char[len+1];
strcpy(str,s.str);
}
String::~String(){
if(str)
delete[] str;
}
istream& operator>>(istream& is,String& s1){
is>>s1.str;
return is;
}
ostream& operator<<(ostream& os,const String& s2){
os<<s2.str;
return os;
}
String String::operator =(const String &o){
delete [] this->str;
this->str=new char[o.len+1];
strcpy(this->str,o.str);
return *this;
}
String String::operator=(char * _str)
{
delete [] this->str;
this->str = new char[strlen(_str)+1];
strcpy(this->str,_str);
return *this;
}
String String::operator +(char *_str){
String s;
s.len=len+strlen(_str);
s.str=new char[s.len];
strcpy(s.str,this->str);
strcat(s.str,str);
return s;
}
String String::operator +(const String& d){
return strcpy(this->str,d.str);
}
String::operator >(const String& b){
return strcmp(this->str,b.str);
}
int main()
{
String s1,s2,s3;
cin>>s1>>s2;
s3=s1+s3;
cout<<s3<<endl;
//for(int i=0;i<s3.length();i++)
//cout<<s3[i];
cout<<endl;
//比较两个字符串的大小,按字符顺序
if(s1>s2)
cout<<s1<<"is more large than "<<s2<<endl;
//输出s2在s3中位置
//cout<<s3.find(s2);
return 0;
}
编译时候结果如下:(xp+vc++6.0)
Compiling...
string.cpp
E:\xuxu\1\string.cpp(50) : error C2248: 'str' : cannot access private member declared in class 'String'
E:\xuxu\1\string.cpp(32) : see declaration of 'str'
E:\xuxu\1\string.cpp(54) : error C2248: 'str' : cannot access private member declared in class 'String'
E:\xuxu\1\string.cpp(32) : see declaration of 'str'
E:\xuxu\1\string.cpp(117) : error C2593: 'operator >>' is ambiguous
E:\xuxu\1\string.cpp(119) : error C2593: 'operator <<' is ambiguous
E:\xuxu\1\string.cpp(125) : error C2593: 'operator <<' is ambiguous
Error executing cl.exe.
string.obj - 5 error(s), 0 warning(s)
我有点搞不顶了
发表于 2009-11-3 03:34:28 | 显示全部楼层 IP:江苏扬州
呵呵,不太清楚
帮顶
回复

使用道具 举报

发表于 2009-11-3 03:34:29 | 显示全部楼层 IP:江苏扬州
http://www.bc-cn.net/bbs/dispbbs.asp?boardID=56&ID=75463&page=1
看看是不是
回复

使用道具 举报

发表于 2009-11-3 03:34:30 | 显示全部楼层 IP:江苏扬州
class String{
friend istream& operator>>(istream& is,String& s1)
{is>>s1.str;return is;}
friend ostream& operator<<(ostream& os,const String& s2)
{os<<s2.str;return os;}
你上面部分的部分改成这样。下面把那两个定义删掉,就可以了
回复

使用道具 举报

发表于 2009-11-3 03:34:34 | 显示全部楼层 IP:江苏扬州
建议重载复值运算符也返回引用,可以a=b=c;连续使用。
回复

使用道具 举报

发表于 2009-11-3 03:34:35 | 显示全部楼层 IP:江苏扬州
谢谢各位了
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

QQ|小黑屋|最新主题|手机版|微赢网络技术论坛 ( 苏ICP备08020429号 )

GMT+8, 2024-9-30 13:25 , Processed in 0.215804 second(s), 12 queries , Gzip On, MemCache On.

Powered by Discuz! X3.5

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表