gpt4 book ai didi

c++ - 重载问题 C++

转载 作者:行者123 更新时间:2023-11-28 01:41:05 25 4
gpt4 key购买 nike

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

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+在上面的代码中运行,但我怀疑它(正确地)返回一个 Set按值(value)。此代码被翻译成

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

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

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

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

添加的const这里让最后一个参数绑定(bind)到任何东西,包括临时变量,这样你就可以打印出 s1 + s2安全地。另外,它(正确地)向调用者表明打印出 Set 的行为。实际上不会改变那个集合。

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

关于c++ - 重载问题 C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47117438/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com