gpt4 book ai didi

operator= 函数上的 C++ gcc 错误

转载 作者:行者123 更新时间:2023-11-28 03:39:32 25 4
gpt4 key购买 nike

函数定义

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

导致错误的行,错误信息引用第327行,从nodeType....开始

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; 结尾

关于operator= 函数上的 C++ gcc 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9752781/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com