【问题标题】:Overloadig + operator must take either no or single argument linklist重载 + 运算符必须采用无参数或单参数链表
【发布时间】:2015-06-04 05:43:08
【问题描述】:

Set.h 文件包含在公共类中:

 friend const Set operator +(const Set & a, const Set & b);

Set.cpp 文件包含一个名为:

const Set Set::operator +(const Set & a, const Set & b)

为什么会出现这个错误:'const Set Set::operator+(const Set&, const Set&)' must take either zero or one argument'

-编辑-

正如你们中的几个建议的那样,删除我的 .h 文件前面的朋友会导致另外两个错误

Set.h:23:53: 错误:'const Set Set::operator+(const Set&, const Set&)' 必须采用零或一个参数 const Set 运算符 +(const Set & a, const Set & b);

Set.cpp:73:55: 错误:'const Set Set::operator+(const Set&, const Set&)' 必须采用零或一个参数 const Set Set::operator +(const Set & a, const Set & b){

-编辑2-

const Set operator +(const Set & a, const Set & b){
Node * intersection; 
while(a != nullptr && b != nullptr){
   if(a->value < b->value){
      intersection -> value = a;
      intersecioon = intersection->next;
      a=a->next;
   }
   if(a->value > b->value){
      intersection -> value = b;
      intersecioon = intersection->next;
      b=b->next;
   }
}
while(a != nullptr){
      intersection -> value = a;
      intersecioon = intersection->next;
      a=a->next;
}
while(b != nullptr){
      intersection -> value = b;
      intersecioon = intersection->next;
      b=b->next;
}
return intersection;
}

这是我的操作员函数。

【问题讨论】:

  • 不要删除friend,从第二行删除Set::,即在cpp文件中,定义为const Set operator+(const Set &amp; a, const Set &amp; b)。如果仍有问题,请发布 MCVE stackoverflow.com/help/mcve
  • 更多错误,添加功能了解更多详情

标签: c++ linked-list operator-overloading overloading


【解决方案1】:

根据Set.h中的声明,operator+Set的非会员好友函数。在Set.cpp 中,您尝试像定义成员函数一样定义它。正确的定义应该有这个签名:

const Set operator +(const Set & a, const Set & b)

【讨论】:

    【解决方案2】:

    只需从.cpp 文件的定义中删除Set::

    头文件将operator+ 声明为非成员函数,因此没有理由在声明中以Set:: 为前缀。这将需要定义类类型 Set 的成员函数。

    【讨论】:

    • @user3249265 比如?
    • 'const Set operator+(const Set&, const Set&)': Set.cpp:75:12: error: no match for 'operator!=' (operand types are 'const Set' and 'std ::nullptr_t')
    • @user3249265 这与您发布的代码无关。
    猜你喜欢
    • 2020-10-15
    • 2018-04-25
    • 2012-11-12
    • 1970-01-01
    • 1970-01-01
    • 2018-04-24
    • 2014-06-13
    • 2019-11-12
    • 1970-01-01
    相关资源
    最近更新 更多