brand_guy 2022-06-30 20:15 采纳率: 57.1%
浏览 34
已结题

关于#运算符#的问题,如何解决?

在定义的class中重载运算符<<报错,而样例中用同样的格式重载,却没有问题。
#ifndef A
#ifndef A
#define A
#include<fstream>
#include<vector>
#include<iostream>
#include<algorithm>
#include"Pell_iterator.h"
using namespace std;
class Pell{
    friend class Pell_iterator;
    friend ostream& operator<<(ostream& os,const Pell& rhs );
    public:
        Pell(int bp=1,int len=1);
        int size()const{return _length;};//返回对象序列的长度
        int beg_pos()const{return _beg_pos;};//返回对象的开始位置
        void change(int bp=1,int len=1);//重新设置对象
        bool next(int& val)const;//将要操作的下一数字赋给val,同时“光标”后移一位
        void next_reset()const{_next=_beg_pos-1;};//重置将要操作的下一数字至开始位置
        int elem(int pos)const;//返回对象序列中第pos个数字
        int sum()const;//求对象序列的和
        bool is_elem(int val);//判断val数值是否包含在对象的序列中
        void display(ostream& os=cout);//用于输出对象包含的序列
        static void generate();//用于填充数列
        Pell& copy(const Pell);
        
        
        typedef Pell_iterator iterator;
        iterator begin()const
        {
            iterator it(_beg_pos);
            return it;
        }
        iterator end()const
        {
            iterator it(_beg_pos+_length);
            return it;
        }
    private:

        static const int _max_size=40;
        int _length;
        int _beg_pos;
        mutable int _next;
        static vector<int> _elems;
};
ostream&
operator<<(ostream& os,const Pell& rhs )
{
    cout<<'('<<rhs._beg_pos<<','<<rhs._length <<") ";
    rhs.display(os);
    return os; 
 } 
#endif  

#include"Pell.h"
#include<iostream>
using namespace std;

int main()
{
    Pell it(4,3);
    cout<<it;
  return 0;
}
报错no match for 'operator'以及下面还有几百个看不懂的报错提示,但是只看重载运算符<<部分的代码,又没找出问题
  • 写回答

2条回答 默认 最新

  • 真相重于对错 2022-07-01 07:37
    关注
    
    void display(ostream& os=cout);
    改成
    void display(ostream& os=cout)const;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 7月9日
  • 已采纳回答 7月1日
  • 创建了问题 6月30日