【问题标题】:C++ gcc error on operator= function运算符 = 函数上的 C++ gcc 错误
【发布时间】:2012-03-17 18:49:36
【问题描述】:

函数定义

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

我假设我在某处定义此方法时出现了某种语法错误。我将如何解决它?谢谢。

【问题讨论】:

  • â&amp;â 标记是否只是在奇怪编码的引号中的 &amp; 标记?
  • @MrLister - 我想是的。可能是复制/粘贴问题。
  • 这不是 & before ::operator 错误吗?
  • @abresas,这是主要问题。第二个问题是 const circularLinkeList& 和 nodeType* 之间的类型不匹配。

标签: c++ gcc compiler-errors


【解决方案1】:

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

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

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

然而,一方面,声明说它返回一个const circularLinkedList&lt;Tp&gt;&amp;。 此外,您不希望在:: 之前有&amp;。它必须是类型的名称,而不是指向该类型变量的指针或引用。如果你想要这种行为,你需要使用代理类。

所以应该是这样的:

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

应该几乎总是以return *this;结尾

【讨论】:

  • 这是一个函数定义,并且确实返回 *this。但你实际上用那条线修复了它。在您回答之前,我不确定如何正确定义函数,也无法使其工作。谢谢!
【解决方案2】:

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

方法声明: 返回const circularLinkedList&lt;Tp&gt;&amp;

方法定义: 返回nodeType&lt;Tp&gt;*

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

定义的标题应该是:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-02-29
    • 1970-01-01
    • 1970-01-01
    • 2016-02-05
    • 2012-03-01
    • 2010-12-06
    • 1970-01-01
    • 2011-01-19
    相关资源
    最近更新 更多