好的,所以我在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++ - 二进制 '==':未找到采用左操作数的运算符,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/12790693/

10-13 08:19
查看更多