重载问题C ++

时间:2017-11-05 01:51:55

标签: c++ operator-overloading

我似乎遇到了一个超载操作员问题。我无法弄清楚错误试图说的是什么。这是错误:

Error: no match for 'operator<<' in
'std::cout << s1.Set::operator+(((const Set&)((const Set*)(& s2))))'

这是我的代码:

#include "Set.cpp"
int main(int argc, char *argv[]){
    Set s1(10),s2(6),s3(3),s4;
    cout<<"First set ({x,y,z}): ";
    cin>>s1;    
    cout<<"A: "<<s1<<endl;
    cout<<"Second set: ";
    cin>>s2;
    cout<<"B: "<<s2<<endl;

    cout<<s1+s2<<endl;

}

class Set {
private:
    bool elements[255];
    int capacity; //Yes, an unsigned char, short, or even size_t, would be better
    Set(const bool elements[255], int capacity); //Helpful for immutable types
public:
    Set();
    Set(short capacity);

    friend std::ostream& operator<<(std::ostream &out, Set &set);
    friend std::istream& operator>>(std::istream &in, Set &set);

    int getCapacity() const; //Cardinality of universe. i.e. |Universe| (or just 'capacity')

};
Set::Set(const bool elements[255], int capacity){
    this->capacity = capacity;
    for(int i=0; i<255;i++){
        if(elements[i] == true && i <= capacity){
            this->elements[i] = true;
        }       
        else{
            this->elements[i] = false;
        }           
    }

}
Set::Set(short capacity){   
    this->capacity = capacity;
}
std::ostream& operator<<(std::ostream &out, Set &set) { 
    int capacity = set.getCapacity();
    out<<"{";   
    for(int i=0; i < 255; i++){     
        if(set.elements[i] == true ){
            out<<i<<",";
        }

    }
    out<<"}";
    return out;
}
std::istream& operator>>(std::istream &in, Set &set) {
    bool arr[255];
    int cap=set.getCapacity();  
    char open;
    in>>open;
    if (in.fail() || open!='{') {
        in.setstate(std::ios::failbit);
        return in;
    }
    for (int i=0;i<cap;i++)
        arr[i]=false;
    std::string buff;
    std::getline(in,buff,'}');
    std::stringstream ss(buff);
    std::string field;
    while (true) {
        std::getline(ss,field,',');
        if (ss.fail()) break;
        int el;
        std::stringstream se(field);        
        se>>el;

        if (el>=0&&el<cap){
            arr[el]=true;           
        }
        else{
            arr[el]=false;
        }
    }
    set=Set(arr,cap);
}
Set Set::operator+(const Set &other) const{
    bool arr[255];

    for(int i=0; i<255;i++){
        if(this->elements[i] == true || other.elements[i]==true)
            arr[i] == true;
    }

    int capacity = this->capacity>=other.capacity?this->capacity:other.capacity; 
    return Set(arr,capacity);
}

我超载+和&gt;&gt;运营商。执行代码时,是否先执行重载+运算符,然后执行&gt;&gt;运营商。

只需要澄清一下。谢谢

1 个答案:

答案 0 :(得分:2)

以下是重载流插入运算符的签名:

friend std::ostream& operator<<(std::ostream &out, Set &set);

请注意,您将最后一个参数作为非const引用。这意味着此函数只能将左值作为其第二个参数。这适用于您列出的所有情况,除了这一个:

cout << s1+s2 << endl;

我不相信您在上面的代码中包含了operator+函数的签名,但我怀疑它(正确)按值返回Set。此代码转换为

(operator<< (cout, s1 + s2)) << endl;

会触发此问题,因为表达式s1 + s2并未评估为左值。

要解决此问题,请更改operator<<功能的签名

friend std::ostream& operator<<(std::ostream &out, const Set &set);

此处添加的const允许最后一个参数绑定到任何内容,包括临时值,这样可以安全地打印出s1 + s2。另外,它(正确地)向呼叫者表明打印出Set的行为实际上不会改变该集合。

顺便说一句,将.cpp文件包含在另一个.cpp文件的顶部是非常奇怪的。您应该为Set类型定义标题并包含该标题。否则,如果多个文件试图包含Set.cpp文件,则由于每个函数的多个定义,您将收到链接器错误。