简体   繁体   English

使用模板链接列表类复制构造函数错误

[英]Copy Constructor Error With a Template Linked List Class

I am doing an assignment on linked lists using a template class. 我正在使用模板类对链接列表进行分配。

In my main.cpp I should be able to create the list (which works) and create another list using either the assignment operator or the copy constructor. 在我的main.cpp中,我应该能够创建列表(有效)并使用赋值运算符或复制构造函数创建另一个列表。 Here is my code: 这是我的代码:

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;
}

The error reads: 该错误显示为:

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: 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 and next are public variables of the Node class. element和next是Node类的公共变量。

Please note, that due to the nature of this assignment I cannot change the class definition only the implementation of the class. 请注意,由于此分配的性质,我不能仅通过类的实现来更改类定义。

EDIT: 编辑:

template <class T>
LinkedList<T>::LinkedList(const LinkedList<T>& other) // I CANNOT CHANGE THIS
{
    // I CAN CHANGE THIS
}

The problem is that you try to call the non-const member function LinkedList<T>::getLeader() for a const object other . 问题是您尝试为const对象other调用非常量成员函数LinkedList<T>::getLeader()

Since the getLeader member function does not modify the object, you can make it const : 由于getLeader成员函数不会修改对象,因此可以将其设置为const:

template <class T>
Node<T>* LinkedList<T>::getLeader() const

If additionally, you want to also prevent that the caller can inadvertently modify the returned node, also make the return type const : 如果另外,还希望防止调用者无意间修改返回的节点,还可以使返回类型为const:

template <class T>
const Node<T>* LinkedList<T>::getLeader() const

In that case, you'll have to adjust the definition of tmp accordingly. 在这种情况下,您必须相应地调整tmp的定义。

If you cannot fix the above issue with the getLeader signature (as indicated by your edit of the question), you have these options left (in order of preference) : 如果您无法使用getLeader签名解决上述问题(如问题的编辑所示),则可以按照优先顺序保留以下选项:

  • use other functionality of the LinkedList class that can work on const objects (like an iterator eg.), assuming such functionality is available 假设可以使用const对象(例如迭代器)上的LinkedList类的其他功能
  • access the head data member directly for other , instead of using the getLeader member function 直接访问head数据成员以获取other成员,而不是使用getLeader成员函数
  • use const_cast to cast away the const-ness of other before calling getLeader on it 使用const_cast抛掷的常量性other之前调用getLeader就可以了

Changing getLeader() 's signature to be const would indeed be the "good" solution to your problem (and, to adhere to standards used in many other contexts, it should probably have been named head() ...), but there is another way to solve your problem given that you're in control of the class itself. getLeader()的签名更改为const确实是解决问题的“好方法”(并且要遵循许多其他情况下使用的标准,它应该应该被命名为head() ...),但是在那里鉴于您可以控制类本身,因此这是解决问题的另一种方法。

Since you're doing this inside the class, you have access to private members as well - that includes private members of other instances of the same class. 由于您是在类中进行此操作的,因此您也可以访问私有成员-包括同一类其他实例的私有成员。 If you look at what getLeader() does, it's probably something like this 1 : 如果您查看getLeader()功能,则可能类似于以下内容1

template<typename T>
class LinkedList {
private:
    Node<T>* head;

public:
    const Node<T>* getLeader() {
        return head;
    }
}

This means that in your copy constructor/assignment operator, you could access other.head directly, instead of doing it via getLeader() . 这意味着在您的复制构造函数/赋值运算符中,您可以直接访问other.head ,而无需通过getLeader() As long as you don't try to change the value of other.head you should be fine. 只要您不尝试更改other.head的值, other.head可以了。


1) Note: untested. 1)注意:未经测试。 I write this off the top of my head, so it might not even compile. 我将此写在脑海中,所以它甚至可能无法编译。 I hope my point comes across even if it doesn't compile... 我希望即使没有编译我也能理解我的观点...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM