注册 登录
编程论坛 C++教室

求助,c++一些问题。

cityrunner 发布于 2016-05-07 23:27, 4202 次点击
有个程序是关于运算符重载的,编译后一直报错,大概就是“=”重载后编译器找不到,求解决;
[Error] no match for 'operator=' (operand types are 'Str' and 'Str')
还有个问题是 函数声明的时候;比如   char& operator[] (int); 返回值类型为啥前面有个&,char& 是个什么意思?
求解;


程序代码:

#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::operator=(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::operator[](int n)
{
    static char ch;
    if(n>strlen(p))
    {
      cout<<"超出边界!"<<endl;   
    return ch;
    }
   
    return *(p+n);
}
Str Str::operator+(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::operator==(Str &str)
{
    if(strcmp(p,str.p)==0)
    return 1;
    else
    return 0;
}

 int Str::operator>(Str &str)

 {
     if(strcmp(p,str.p)>0)
     return 1;
     else
     return 0;
}
int Str::operator<(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;
}
10 回复
#2
cityrunner2016-05-08 10:19
求解啊。
#3
cityrunner2016-05-08 10:22
主要是这

程序代码:
Str& Str::operator=(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::operator[](int n)

 {
     static char ch;
     if(n>strlen(p))
     {
       cout<<"超出边界!"<<endl;   
     return ch;
     }
     
     return *(p+n);

 }

剩下的我没问题,[Error] no match for 'operator=' (operand types are 'Str' and 'Str')
#4
rjsp2016-05-08 17:35
整段代码竟然看不到一个const关键字
#5
cityrunner2016-05-08 23:49
回复 4楼 rjsp
你说的这个是 参数中 应该带吗?
不过为啥在 Devc++ 里边编译不通过呢?
#6
cityrunner2016-05-08 23:50
回复 4楼 rjsp
char& operator[] (int); 返回值类型为啥前面有个&,求解,为啥不能直接char
#7
wengbin2016-05-09 09:33
[type]& func(parameters list),意思是func函数将返回一个type类型的引用,这样的函数一般是在参数列表中传入了一个引用参数,调用之后有修改,将修改过后的参数返回给主调函数。

程序代码:
int & change(int & a/*传入a的引用,意思是不会创建一个新的临时变量,直接作用于a*/)
{
    a=2*a;
    return a;
}
#8
rjsp2016-05-09 15:39
连const关键字都不用?!你应该找本真正的C++书籍学一下

最少你得实现以下函数(随手写的,可能有错误)
程序代码:
#include <iostream>

class String
{
public:
    String();
    String( const char* s );
    String( const String& str);
    ~String();

    String& operator=( const char* s );
    String& operator=( const String& str );

    const char* c_str() const;

    const char& operator[]( size_t index ) const;
    char& operator[]( size_t index );

    String operator+( const char* s ) const;
    String operator+( const String& str ) const;

    String operator+=( const char* s ) const;
    String operator+=( const String& str ) const;

    bool operator==( const char* s ) const;
    bool operator==( const String& str ) const;

    bool operator!=( const char* s ) const;
    bool operator!=( const String& str ) const;

    bool operator>( const char* s ) const;
    bool operator>( const String& str ) const;

    bool operator<( const char* s ) const;
    bool operator<( const String& str ) const;

    bool operator>=( const char* s ) const;
    bool operator>=( const String& str ) const;

    bool operator<=( const char* s ) const;
    bool operator<=( const String& str ) const;

private:
    char* data_;

    friend String operator+( const char* s, const String& str );
    friend bool operator==( const char* s, const String& str );
    friend bool operator!=( const char* s, const String& str );
    friend bool operator>( const char* s, const String& str );
    friend bool operator<( const char* s, const String& str );
    friend bool operator>=( const char* s, const String& str );
    friend bool operator<=( const char* s, const String& str );

    friend std::istream& operator>>( std::istream& is, String& str );
    friend std::ostream& operator<<( std::ostream& os, const String& str );
};

#9
cityrunner2016-05-09 20:23
回复 7楼 wengbin
受教了,谢谢。
#10
cityrunner2016-05-09 20:25
回复 8楼 rjsp
您说这个传递给这个参数是字符串是不是才需要加 const ?
#11
wengbin2016-05-10 14:57
回复 10楼 cityrunner
加const关键字是要求被调函数不得修改对象的私有成员,敢问楼主现在是工作了,参加项目了嘛?
1