重载问题 C++

1011 1110

我似乎遇到了操作员过载的问题。我真的无法弄清楚错误想说什么。这是错误:

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);
}

我重载了 + 和 >> 运算符。执行代码时,它会不会先执行重载的 + 运算符,然后再执行 >> 运算符。

只需要澄清一下。谢谢

模板类型定义

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

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

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

cout << s1+s2 << endl;

我不相信您operator+在上面的代码中包含了函数的签名,但我怀疑它(正确地)返回了一个Setby 值。这段代码被翻译成

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

这会触发问题,因为表达式s1 + s2不会计算为左值。

要解决此问题,请更改operator<<函数 t的签名

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

const此处添加的使最后一个参数绑定到任何对象,包括临时对象,这样您就可以s1 + s2安全地打印出来另外,它(正确地)向调用者表明打印出 a 的行为Set实际上不会改变该集合。

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章