【发布时间】:2015-02-15 03:08:07
【问题描述】:
我有一个链表类List。我必须为它创建一个复制构造函数。我实际上遇到了语法问题。这是我的代码:
template <class T>
List<T>::List(const List<T>& otherList)
{
}
otherList 是 List 类型的另一项。事实上,我认为它是一个指向列表的指针。我为 List 类定义了一个 getHead() 函数,它工作正常。 我认为执行以下操作就可以了:
Node* temp = otherList->getHead();
node 是组成 List 的指针。这是一个单独的类节点。 但执行上述操作会出现错误:
base operand of -> has non-pointer type const List<int>
所以我尝试使用“。”而不是上面表达式中的“->”。得到以下错误:
passing const List<int> as this argument of ListItem<T>* List<T>::getHead() [with T = int] discards qualifiers [-fpermissive]
我的 getHead 函数:
template <class T>
node<T>* List<T>::getHead()
{
return head;
}
请指导我
【问题讨论】:
-
是什么让你认为它是指针?
-
外部列表引用为 const。因此您只能使用它的 const 成员,并且您从中检索到的数据成员是相同的。
标签: c++ linked-list