Overloading+运算符必须采用无参数链表或单参数链表

Overloadig + operator must take either no or single argument linklist

本文关键字:参数 链表 单参数 运算符 Overloading+      更新时间:2023-10-16

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

-编辑-

正如你们几个人所建议的,删除我的.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::运算符+(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;
}

这是我的操作员功能。

根据Set.h中的声明,operator+Set的非成员友元函数。在Set.cpp中,您试图将其定义为定义成员函数。正确的定义应该有这样的签名:

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

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

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