二进制'==':未找到采用左操作数的运算符
binary '==' : no operator found which takes a left-hand operand
好的,所以我在堆栈溢出上阅读了这个问题的多个实例,但我似乎找不到与我的项目相关的答案。找到采用左手操作数的二进制无运算符
我似乎无法弄清楚为什么我可以在 2 个整数上使用相等运算符,但不能在编写该程序的两个对象文件之间使用相等运算符。
这是该类的截图:
template <class Type>
class stackType: public stackADT<Type>
{
public:
const stackType<Type>& operator=(const stackType<Type>&);
stackType(const stackType<Type>& otherStack);
~stackType();
bool operator== (const stackType<Type>&) const;
private:
int maxStackSize; //variable to store the maximum stack size
int stackTop; //variable to point to the top of the stack
Type *list; //pointer to the array that holds the stack elements
};
template<class Type>
bool stackType<Type>::operator==(const stackType<Type & right) const
{
//assuem the stacks have same number of elements
if(this->stacKTop != right.stackTop)
return false;
//check for eqaulity
for (int i=0; i<stackTop; i++)
if (this->list[i] != right.list[i])
return false;
return true;
}//end operator function
要测试的主程序:
using namespace std;
int main()
{
stackType<int> s1(12);
stackType<int>s2(15);
for (int i=3; i<30; i+=3)
{
s1.push(i);
s2.push(i);
}//end for
if(s1 == s2)
cout<<"both stacks are equal"<<endl;
else
cout<<"stacks are not equal"<<endl;
}
我正在使用Visual Studio 2012,我只是感到困惑和倦怠。 我尝试更改声明,我将关键字 const 添加到声明中,我添加了更多参数,什么都没有。 我在两个整数上测试了相等运算符,它编译并工作。 再次,我想从我的混乱中学习,所以欢迎所有的意见。
如果您将public stackADT<Type>
定义为空类,则代码有效,这意味着它可能不应该存在,或者您对代码有一些其他的诡异之处。
就语法而言:在(const stackType<Type & right) const
你错过了一个>
. 此外,您一度将stackTop
拼写错误为stacKTop
。
相关文章:
- 为什么C++逐位AND运算符在不同大小的操作数中表现为这样
- 错误 C2679:二进制"<<":未找到采用类型 'std::string_view' 的右侧操作数的运算符(或者没有可接受的转换)
- 是否所有C++运算符都接受操作数?
- 一元*运算符的操作数是否期望一个 prvalue
- C2678 二进制 '==':未找到采用 'Card' 类型左操作数的运算符(或者没有可接受的转换)
- 我的运算符重载是否有效<<(流插入)左操作数不是 ostream
- 没有运算符"="与这些操作数匹配,JUCE 窗口演示应用程序
- C2679 二进制"<<":未找到采用右操作数类型的运算符
- CPP 运算符重载操作数位置 [] 与 ==
- int* 和 int[] 类型对二进制运算符 + 的操作数无效
- 运算符'<'不能应用于类型 'string' 和 'string' 的操作数
- 错误 C2679 二进制"<<":未找到采用类型为 'T' 的右侧操作数的运算符
- 是否允许三元运算符在C++中计算两个操作数?
- 二进制 '==':未找到采用 'Enemy' 类型左侧操作数的运算符(或者没有可接受的转换)
- 与"运算符>>"不匹配(操作数类型为"QDataStream"和"QJsonObject")
- 类型"int"和"const char [15]"到二进制"运算符<<"的无效操作数^
- 如何摆脱逗号运算符的这个错误右操作数没有效果( wunsued 值)?
- c++ 错误:二进制"运算符+"类型"矩阵*"和"矩阵*"的操作数无效
- c++运算符重载-我实际返回的操作数类型是什么
- 没有运算符>>与这些操作数匹配 ifstream