模板链表类的复制构造函数错误
Copy Constructor Error With a Template Linked List Class
我正在使用模板类对链表进行赋值。
在我的main.cpp中,我应该能够创建列表(有效),并使用赋值运算符或复制构造函数创建另一个列表。这是我的代码:
template <class T>
LinkedList<T>::LinkedList(const LinkedList<T>& other)
{
Node<T>* tmp = other.getLeader(); //LINE WHERE THE ERROR OCCURS
for(int i = 0; tmp != NULL; i++)
{
insert(i, tmp->element);
tmp = tmp->next;
}
}
template <class T>
Node<T>* LinkedList<T>::getLeader()
{
return head;
}
错误显示:
linkedList.C:61:6: error: passing ‘const LinkedList<int>’ as ‘this’ argument
of ‘Node<T>* LinkedList<T>::getLeader() [with T = int]’
discards qualifiers [-fpermissive] tmp = other.getLeader();
Main.cpp:
int main()
{
LinkedList<int> list;
list.insert(0, 0);
list.insert(1, 1);
list.insert(2, 2);
cout << list;
LinkedList<int> list2(list);
cout << list2;
return 0;
}
element和next是Node类的公共变量。
请注意,由于此作业的性质,我不能仅更改类的实现来更改类的定义。
编辑:
template <class T>
LinkedList<T>::LinkedList(const LinkedList<T>& other) // I CANNOT CHANGE THIS
{
// I CAN CHANGE THIS
}
问题是您试图为常量对象other
调用非常量成员函数LinkedList<T>::getLeader()
。
由于getLeader
成员函数不修改对象,因此可以将其设为const:
template <class T>
Node<T>* LinkedList<T>::getLeader() const
此外,如果您还想防止调用者无意中修改返回的节点,也可以将返回类型设为const:
template <class T>
const Node<T>* LinkedList<T>::getLeader() const
在这种情况下,您将不得不相应地调整tmp
的定义。
如果您无法用getLeader
签名解决上述问题(如您对问题的编辑所示),您可以选择以下选项(按偏好顺序):
- 使用
LinkedList
类中可以处理常量对象的其他功能(例如迭代器),假设这些功能可用 - 直接访问
other
的head
数据成员,而不是使用getLeader
成员函数 - 在调用
getLeader
之前,使用const_cast
丢弃other
的常量
将getLeader()
的签名更改为const
确实是解决问题的"好"方法(而且,为了遵守许多其他上下文中使用的标准,它可能应该命名为head()
…),但考虑到您可以控制类本身,还有另一种方法可以解决问题。
由于您在类内执行此操作,因此您也可以访问私有成员,其中包括同一类的其他实例的私有成员。如果你看看getLeader()
的作用,它可能是这样的1:
template<typename T>
class LinkedList {
private:
Node<T>* head;
public:
const Node<T>* getLeader() {
return head;
}
}
这意味着在复制构造函数/赋值运算符中,您可以直接访问other.head
,而不是通过getLeader()
进行访问。只要你不尝试更改other.head
的值,你就应该没事。
1)注意:未经测试。我把这个写在脑子里,所以它甚至可能不会编译。我希望我的观点能得到理解,即使它没有编译
相关文章:
- C++17复制构造函数,在std::unordereded_map上进行深度复制
- 为什么在C++中使用私有复制构造函数与删除复制构造函数
- 当从函数参数中的临时值调用复制构造函数时
- 如果有一个模板构造函数只有一个泛型参数,为什么我必须有一个复制构造函数
- 为什么需要复制构造函数,在哪些情况下它们非常有用
- 使用仅使用一次的变量调用的复制构造函数.这可能是通过调用move构造函数进行编译器优化的情况吗
- 为什么类中的ostringstream类型的成员会导致";调用隐含删除复制构造函数";错误
- 复制构造函数、赋值运算符C++
- std::ofstream 作为类成员删除复制构造函数?
- 复制构造函数C++无法正确复制指针
- 关于复制构造函数的一个棘手问题
- 为什么调用复制构造函数而不是移动构造函数?
- 填充上编译器生成的复制构造函数之间的不一致
- C++ 对象指针数组的复制构造函数
- C++ 基本 CTOR 说明 - 为什么不调用赋值/复制构造函数
- 防止在复制构造函数中隐式调用基构造函数
- 为用户定义的类正确调用复制构造函数/赋值运算符
- 具有已删除移动和复制构造函数的类的就地构造
- 复制构造函数隐式转换问题
- 复制构造函数中的递归调用