我不完全确定为什么我得到这个消息,考虑到方括号不是一个运算符,但在这里,我们走了。没有匹配运算符[] C++
在我的IntSet.h
文件中,我声明了一个看起来像这样的特定函数。
IntSet unions(const IntSet& operand)const;
该文件由IntSet
叫,另需IntSet
作为参数,并返回两个集合的并集。
在我的IntSet.cpp
文件中,它看起来像这样。
IntSet IntSet::unions(const IntSet& operand) const
{
IntSet returnSet(50);
for (int i = 0; i < 50; i++){
if (setArray[i] == 1 || operand[i]==1){
if(setArray[i] == 1){
returnSet.addElement(setArray[i]);
}
else if(operand[i] == 1){
returnSet.addElement(operand[i]);
}
}
}
return returnSet;
}
我得到的错误来自operand[I] == 1
。
IntSet.cpp|52|error: no match for 'operator[]' (operand types are 'const IntSet' and 'int')
。
该程序不能覆盖和操作员启动。我还应该提到IntSet unions(const IntSet& operand)const;
已提供,也不能更改。
任何方向,我应该如何解决这个问题?
的std ::集没有const的operator []的过载能力:用户.AT()函数,而不是 – zapredelom
你必须重载'INTSET ::运算符[]'因为你要拨打运营商'[]'就可以了。 – songyuanyao
运算符重载不允许以任何形式。我也不能更改类方法声明或参数。 – Podo