| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 3872 人关注过本帖
标题:求助,c++一些问题。
只看楼主 加入收藏
cityrunner
Rank: 2
等 级:论坛游民
帖 子:26
专家分:33
注 册:2016-1-22
结帖率:100%
收藏
已结贴  问题点数:15 回复次数:10 
求助,c++一些问题。
有个程序是关于运算符重载的,编译后一直报错,大概就是“=”重载后编译器找不到,求解决;
[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;
}
2016-05-07 23:27
cityrunner
Rank: 2
等 级:论坛游民
帖 子:26
专家分:33
注 册:2016-1-22
收藏
得分:0 
求解啊。
2016-05-08 10:19
cityrunner
Rank: 2
等 级:论坛游民
帖 子:26
专家分:33
注 册:2016-1-22
收藏
得分:0 
主要是这

程序代码:
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')
2016-05-08 10:22
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:528
帖 子:9007
专家分:53942
注 册:2011-1-18
收藏
得分:0 
整段代码竟然看不到一个const关键字
2016-05-08 17:35
cityrunner
Rank: 2
等 级:论坛游民
帖 子:26
专家分:33
注 册:2016-1-22
收藏
得分:0 
回复 4楼 rjsp
你说的这个是 参数中 应该带吗?
不过为啥在 Devc++ 里边编译不通过呢?
2016-05-08 23:49
cityrunner
Rank: 2
等 级:论坛游民
帖 子:26
专家分:33
注 册:2016-1-22
收藏
得分:0 
回复 4楼 rjsp
char& operator[] (int); 返回值类型为啥前面有个&,求解,为啥不能直接char
2016-05-08 23:50
wengbin
Rank: 10Rank: 10Rank: 10
来 自:陕西西安
等 级:贵宾
威 望:19
帖 子:370
专家分:1846
注 册:2015-5-8
收藏
得分:10 
[type]& func(parameters list),意思是func函数将返回一个type类型的引用,这样的函数一般是在参数列表中传入了一个引用参数,调用之后有修改,将修改过后的参数返回给主调函数。

程序代码:
int & change(int & a/*传入a的引用,意思是不会创建一个新的临时变量,直接作用于a*/)
{
    a=2*a;
    return a;
}
2016-05-09 09:33
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:528
帖 子:9007
专家分:53942
注 册:2011-1-18
收藏
得分:5 
连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 );
};

2016-05-09 15:39
cityrunner
Rank: 2
等 级:论坛游民
帖 子:26
专家分:33
注 册:2016-1-22
收藏
得分:0 
回复 7楼 wengbin
受教了,谢谢。
2016-05-09 20:23
cityrunner
Rank: 2
等 级:论坛游民
帖 子:26
专家分:33
注 册:2016-1-22
收藏
得分:0 
回复 8楼 rjsp
您说这个传递给这个参数是字符串是不是才需要加 const ?
2016-05-09 20:25
快速回复:求助,c++一些问题。
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.024469 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved