重载+运算符必须采用no或单个参数链表

时间:2015-03-31 01:46:40

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

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&)'必须采用零或一个参数'

-edit -

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

Set.h:23:53:错误:'const Set Set :: operator +(const Set&,const Set&)'必须带零或一个参数     const设置运算符+(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){

-edit 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;
}

这是我的操作员功能。

2 个答案:

答案 0 :(得分:2)

根据Set.h中的声明,operator+Set的非会员友情功能。在Set.cpp中,您试图像定义成员函数一样定义它。正确的定义将具有此签名:

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

答案 1 :(得分:0)

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

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