为什么我不能在复制构造函数中更改实例的私有数据,如果它是 const 的?

Why can't I change my private data of an instance in copy constructor if it's const?

本文关键字:数据 const 如果 实例 不能 复制 构造函数 为什么      更新时间:2023-10-16

所以我正在制作一个链表,其中我已经成功地实现了插入方法。但接下来我必须将 OrderedLinkedList 实例数据复制到一个新的空实例。问题是当我尝试访问空 LinkedList 的"头"指针时,它说我无法访问头,因为它是常数。以下是错误的完整说明。

完全错误

>------ Build started: Project: betterLinkedList, Configuration: Debug Win32 ------
1>  assign3_driver.cpp
1>c:usersw01104311downloadsassign3 (1)assign3orderedlinkedlist.h(67): error C3490: 'head' cannot be modified because it is being accessed through a const object
1>          c:usersw01104311downloadsassign3 (1)assign3orderedlinkedlist.h(64) : while compiling class template member function 'void OrderedLinkedList<Type>::copy(const OrderedLinkedList<Type> &)'
1>          with
1>          [
1>              Type=MemberDO
1>          ]
1>          c:usersw01104311downloadsassign3 (1)assign3orderedlinkedlist.h(60) : see reference to function template instantiation 'void OrderedLinkedList<Type>::copy(const OrderedLinkedList<Type> &)' being compiled
1>          with
1>          [
1>              Type=MemberDO
1>          ]
1>          c:usersw01104311downloadsassign3 (1)assign3orderedlinkedlist.h(58) : while compiling class template member function 'OrderedLinkedList<Type>::OrderedLinkedList(const OrderedLinkedList<Type> &)'
1>          with
1>          [
1>              Type=MemberDO
1>          ]
1>          c:usersw01104311downloadsassign3 (1)assign3assign3_driver.cpp(95) : see reference to function template instantiation 'OrderedLinkedList<Type>::OrderedLinkedList(const OrderedLinkedList<Type> &)' being compiled
1>          with
1>          [
1>              Type=MemberDO
1>          ]
1>          c:usersw01104311downloadsassign3 (1)assign3assign3_driver.cpp(42) : see reference to class template instantiation 'OrderedLinkedList<Type>' being compiled
1>          with
1>          [
1>              Type=MemberDO
1>          ]
1>c:usersw01104311downloadsassign3 (1)assign3orderedlinkedlist.h(74): error C2664: 'Node<Type>::Node(const Node<Type> &)' : cannot convert parameter 1 from 'MemberDO' to 'const Node<Type> &'
1>          with
1>          [
1>              Type=MemberDO
1>          ]
1>          Reason: cannot convert from 'MemberDO' to 'const Node<Type>'
1>          with
1>          [
1>              Type=MemberDO
1>          ]
1>          No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
1>c:usersw01104311downloadsassign3 (1)assign3orderedlinkedlist.h(79): error C2664: 'Node<Type>::Node(const Node<Type> &)' : cannot convert parameter 1 from 'MemberDO' to 'const Node<Type> &'
1>          with
1>          [
1>              Type=MemberDO
1>          ]
1>          Reason: cannot convert from 'MemberDO' to 'const Node<Type>'
1>          with
1>          [
1>              Type=MemberDO
1>          ]
1>          No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
1>  Generating Code...
1>  Compiling...
1>  MemberDO.cpp
1>  Generating Code...
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

那么我该如何更改这个新的链接列表数据(例如更改头部指针和尾部指针)?这是我的代码:

   #ifndef ORDEREDLINKEDLIST_H
    #define ORDEREDLINKEDLIST_H
    template <class Type>
    struct Node
    {
        Node<Type>();
        Type info;
        Node*next;
    };

    template <class Type>
    class OrderedLinkedList
    {
    public:
        OrderedLinkedList();
        OrderedLinkedList(const OrderedLinkedList& other);
        ~OrderedLinkedList();
        OrderedLinkedList<Type>& operator=(const OrderedLinkedList<Type>& other);

        int insert(const Type&);
        Type* find(int) const;
        Type* get(int) const;
        int remove(int);
            void clear();
            int size() const;
        void print() const;
    private:
        void copy(const OrderedLinkedList<Type>& other);
        int length;
        Node<Type>* head;
        Node<Type>* tail;
    };

    template <class Type>
    Node<Type>::Node(){
        next = NULL;
    }
    template <class Type>
    OrderedLinkedList<Type>::OrderedLinkedList()
    {
        head = NULL;
        tail = NULL;
        length = 0;
    }
    template <class Type>
    OrderedLinkedList<Type>::OrderedLinkedList(const OrderedLinkedList& other)
    {
        copy(other);
    }
    template <class Type>
    void OrderedLinkedList<Type>::copy(const OrderedLinkedList& other){
        length = other.length;
    if( other.head = NULL){
        head = NULL;
        tail = NULL;
        return;
    }
    head = new Node<Type>(other.head->info);
    Node<Type>* current = other.head->next;
    Node<Type>* node = head;
    while(current!=NULL){
        node->next = new Node<Type>(current->info);
        if(current->next = NULL){
        tail = current;
        }
        current = current->next;
        node = node->next;
    }

    }
    template <class Type>
    OrderedLinkedList<Type>& OrderedLinkedList<Type>::operator=(const OrderedLinkedList& other)
    {
    return *this;
    }
    template <class Type>
    int OrderedLinkedList<Type>::insert(const Type& item)
    {
        Node<Type>* newNode = new Node<Type>();
        Node<Type>* tmp = new Node<Type>();
        Node<Type>* prev = new Node<Type>(); 
        newNode->info = item;
        bool firstNode = true;

        if(head !=NULL){
            tmp = head;
            while(tmp != NULL && newNode->info.getKey()>tmp->info.getKey()){//put keys in order
                if(firstNode){
                    firstNode = false;
                }
                prev = tmp;//keeps track of previous node
                tmp = tmp->next;
            }

            if(firstNode){//if first item in the list
                head = newNode;
            }else{prev->next = newNode;}
            if(tmp == NULL){//if last item in the list
                newNode->next = NULL;
            }else{newNode->next = tmp;}
        }else{
        head = newNode;
        }

        while(newNode->next != NULL){
            newNode = newNode->next;
        }
        tail = newNode;
        length++;


        return newNode->info.getKey();
    }
    template<class Type>
    Type* OrderedLinkedList<Type>::get(int dest) const
    {
        if(dest>length-1 || dest < 0){
            return NULL;
        }
        Node<Type>*curr = new Node<Type>;
        int currCount = 0;
        curr = head;
        while(currCount < dest){
            curr = curr->next;
            currCount++;
        }
        return &(curr->info);//did not delete curr
    }
    template <class Type>
    Type* OrderedLinkedList<Type>::find(int key) const
    {
        if(key < 0){return NULL;}//checks if negative, if it is return NULL
        Node<Type>*curr = new Node<Type>;
        int currCount = 0;
        curr = head;
        while(curr != NULL && curr->info.getKey() != key){
            curr = curr->next;
            currCount++;
        }
        if(curr==NULL){
        return NULL;
        }
            return &(curr->info);
    }
    template <class Type>
    int OrderedLinkedList<Type>::remove(int key)
    {
    return 0;
    }
    template <class Type>
    void OrderedLinkedList<Type>::clear()
    {
        head = NULL;
        tail = NULL;
        length = 0;
    }

    template <class Type>
    int OrderedLinkedList<Type>::size() const
    {
        return length;
    }
    template <class Type>
    void OrderedLinkedList<Type>::print() const
    {
    }
    #endif

这是来自驱动程序源文件的调用

OrderedLinkedList<MemberDO> copy(memberList);

有更多的代码可以保存数据,但我认为在这种情况下不需要它。不过我可能是错的,所以如果您需要更多代码,请告诉我。提前感谢您的帮助!!

您的Node类模板没有采用Type(其模板参数)的构造函数,但您反复将其视为:

例如:

head = new Node<Type>(other.head->info);

和:

node->next = new Node<Type>(current->info);

唯一的单个参数构造函数是实现定义的复制和移动构造函数,它将采用Node<Type>而不是Type(或严格意义上对Node<Type>的某种形式的引用)。