gpt4 book ai didi

c++ - 二进制 '==' : no operator found which takes a left-hand operand

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:30:38 24 4
gpt4 key购买 nike

好吧,我已经在 Stack Overflow 上阅读了这个问题的多个实例,但我似乎找不到与我的项目相关的答案。 binary-no-operator found which takes left handed 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++ - 二进制 '==' : no operator found which takes a left-hand operand,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12790693/

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