二进制'==':未找到采用左操作数的运算符

binary '==' : no operator found which takes a left-hand operand

本文关键字:操作数 运算符 二进制      更新时间:2023-10-16

好的,所以我在堆栈溢出上阅读了这个问题的多个实例,但我似乎找不到与我的项目相关的答案。找到采用左手操作数的二进制无运算符

我似乎无法弄清楚为什么我可以在 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

相关文章: