免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 661 | 回复: 0
打印 上一主题 下一主题

[C++] c++ 小问题求助 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2016-05-08 01:11 |只看该作者 |倒序浏览
#include<iostream>
#include<cstdlib>
#include<cstring>
using namespace std;
class Str
{
        char *p;
public:
   Str();
   Str(char *s);
   ~Str();
   Str(Str&;       
   Str& operator=(Str&;
   char& operator[] (int);
   Str operator+(Str&;
   int operator==(Str&;
   int operator>(Str&;
   int operator<(Str&;
   friend istream& operator>>(istream&,Str&;
   friend ostream& operator<<(ostream&,Str&;
   
} ;
Str::Str()
{
        p=NULL;
}
Str::Str(char *s)
{
        p=new char[strlen(s)+1];
        strcpy(p,s);
}
Str::~Str()
{
        if(p)
        delete []p;
}
Str::Str(Str &str)
{
        if(str.p)
        {
                p=new char[strlen(str.p)+1];
                strcpy(p,str.p);
        }
        else
        p=NULL;
}
Str& Str:perator=(Str &str)
{
        if(this==&str)
        return *this;
        if(p)
        delete []p;
        p=new char[strlen(str.p)+1];
        strcpy(p,str.p);
        return *this;
}
char& Str:perator[](int n)
{
        static char ch;
        if(n>strlen(p))
        {
      cout<<"超出边界!"<<endl;       
        return ch;
        }
       
        return *(p+n);
}
Str Str:perator+(Str &str)
{
        Str stradd;
        stradd.p=new char[strlen(p)+strlen(str.p)+1];
        strcpy(stradd.p,p);
        strcat(stradd.p,str.p);
        return stradd;
}
int Str:perator==(Str &str)
{
        if(strcmp(p,str.p)==0)
        return 1;
        else
        return 0;
}
int Str:perator>(Str &str)
{
        if(strcmp(p,str.p)>0)
        return 1;
        else
        return 0;
}
int Str:perator<(Str &str)
{
   if(strcmp(p,str.p)<0)
   return 1;
   else
   return 0;       
}
istream& operator>>(istream &in,Str &str)
{
        char s[100];
        cout<<"请输入字符串对象的内容:";
        in.getline(s,100);
        if(str.p)
        delete []str.p;
        str.p=new char[strlen(s)+1];
        strcpy(str.p,s);
    return in;
}
ostream& operator<<(ostream &out,Str &str)
{
  out<<str.p;
  return out;       
       
}
int main()
{
        Str s1,s2,s3;
        cout<<"字符操作演示!"<<endl;
        cout<<"输入两个字符串类的对象,观察运算结果!"<<endl;
        cin>>s1>>s2;
        s3=s1+s2;
        cout<<s1<<'+'<<s2<<'='<<s3<<endl;
        if(s1>s2)
        cout<<s1<<'>'<<s2<<endl;
        else if(s1<s2)
        cout<<s1<<'<'<<s2<<endl;
        else
        cout<<s1<<'='<<s2<<endl;
        cout<<"实现下标运算符操作"<<endl;
         int n;
        cout<<"请输入一个整数:";
        cin>>n;
         cout<<"s1["<<n<<"]="<<s1[n]<<endl;
         cout<<"s2["<<n<<"]="<<s2[n]<<endl;
         s1[1]='a';
         s2[1]='b';
         cout<<"s1="<<s1<<endl;
         cout<<"s2="<<s2<<endl;
         return 1;
}
求助,c++一些问题。
有个程序是关于运算符重载的,编译后一直报错,大概就是“=”重载后编译器找不到,求解决;
[Error] no match for 'operator=' (operand types are 'Str' and 'Str')
还有个问题是 函数声明的时候;比如   char& operator[] (int); 返回值类型为啥前面有个&,char& 是个什么意思?为什么不直接用char
求解;
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP