【问题标题】:binary '==' : no operator found which takes a left-hand operand二进制 '==' : 没有找到接受左操作数的运算符
【发布时间】:2012-10-08 23:03:23
【问题描述】:

好的,我在 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 添加到声明中,我添加了更多参数,什么都没有。我在两个整数上测试了相等运算符,它编译并工作。再次,我想从我的混乱中学习,所以欢迎所有的意见。

【问题讨论】:

  • 代码墙攻击!为您的问题制作最小示例发生了什么?
  • 我删除了大部分代码来清理它
  • (const stackType&lt;Type &amp; right) const 你错过了&gt;。 @MarkStevens 声明和定义了一个 ==
  • The code works 如果你删除神秘的public stackADT&lt;Type&gt; 并修复一些语法错误。
  • == 运算符在哪里定义为stackType

标签: c++


【解决方案1】:

如果您将 public stackADT&lt;Type&gt; 定义为一个空类,则该代码有效,这意味着它可能不应该存在,或者您对代码有一些其他奇怪之处。

就语法而言:(const stackType&lt;Type &amp; right) const 你错过了&gt;。此外,您曾一度将 stackTop 拼错为 stacKTop

【讨论】:

  • 感谢 Mooing Duck 和 Jesse Good,这已经解决了问题。 public stackADT 应该是我开始时忘记删除的抽象类。再次感谢您
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-07-23
  • 2014-12-04
  • 1970-01-01
  • 2014-05-02
  • 1970-01-01
相关资源
最近更新 更多