运算符=函数上的C++gcc错误

C++ gcc error on operator= function

本文关键字:C++gcc 错误 函数 运算符      更新时间:2023-10-16

函数定义

const circularLinkedList<Tp>& operator=(const circularLinkedList<Tp>& otherList);

导致错误的行,错误消息指的是从nodeType…开始的行327。。。。

template <class Tp>
nodeType<Tp>* circularLinkedList<Tp>&::operator=(const circularLinkedList<Tp>& otherList)

来自gcc编译器的错误消息是:

circularLinkedList.h:327: error: invalid declarator before â&â token
circularLinkedList.h:327: error: expected initializer before â&â token

我认为我在某个地方定义这个方法时犯了某种语法错误。我该怎么着手修理它呢?谢谢

你能为我们发布更多的代码吗?你能解释一下nodeType是什么吗?

下面看起来像是一个函数定义:

template <class Tp>
nodeType<Tp>* circularLinkedList<Tp>&::operator=(const circularLinkedList<Tp>& otherList)

但是,首先,声明说它返回一个const circularLinkedList<Tp>&。此外,您不希望在::之前有&。它需要是类型的名称,而不是指向该类型变量的指针或引用。如果你想要这种行为,你需要使用一个代理类。

所以它应该是这样的:

template <class Tp>
const circularLinkedList<Tp>& circularLinkedList<Tp>::operator=(const circularLinkedList<Tp>& other)

哪个几乎总是以return *this;

结束

在第一个代码块中,显示的是方法声明,而不是函数定义。在第二个代码块中,将显示方法定义的标头。

方法声明:返回const circularLinkedList<Tp>&

方法定义:返回nodeType<Tp>*

您没有定义您声明的方法。您正在定义一些尚未声明的方法。

定义的标题应该是:

const circularLinkedList<Tp>& circularLinkedList<Tp>::operator=(const circularLinkedList<Tp>& otherList)