# Kopierkonstruktor



## LeopoldStotch (24. Nov 2016)

Ich will für meine LinkedList einen Kopierkonstruktor implementieren, doch beim kompilieren bekomme ich immer 2 Fehlermeldungen, dich wohl nicht verstehe. Zumindest denke ich, dass es an der konstanten Referenz liegt liegt.

Wenn mir da wer auf die Schnelle weiterhelfen könnte, wäre das nett 



Spoiler: Fehlermeldungen





```
LinkedList.cpp:9:39: error: passing ‘const LinkedList’ as ‘this’ argument of ‘const char* LinkedList::getFirst()’ discards qualifiers [-fpermissive]
  const char* text = original.getFirst();
                                       ^
LinkedList.cpp:14:24: error: passing ‘const LinkedList’ as ‘this’ argument of ‘const char* LinkedList::get(int)’ discards qualifiers [-fpermissive]
   text = original.get(i);
```






Spoiler: LinkedList.cpp





```
#include <iostream>
#include "LinkedList.h"

LinkedList::LinkedList(): first{nullptr}, last{nullptr} {
    std::cout << "Leere Liste erzeugt." << std::endl;
}

LinkedList::LinkedList(const LinkedList& original) {
    const char* text = original.getFirst();

    for(int i = 1; text != nullptr; i++){
        this->append(text);

        text = original.get(i);
    }
}

LinkedList::~LinkedList(){
    const char* text = this->getFirst();
    const char* condition = "Die Liste ist Leer!";

    for(int i = 0; text != nullptr; i++){
        this->remove(i);

        text = this->get(i);
    }

    std::cout << "Liste gelöscht." << std::endl;
}

int LinkedList::append(const char* text){
    Node* newNode = new Node(last, text);

    if(first == nullptr) first = newNode;

    if(last != nullptr) last->setNext(newNode);

    last = newNode;

    return 1;
}

int LinkedList::insert(const char* text, int p){
    if(first == nullptr) return LinkedList::append(text);

    int i;
    Node* tmp = first;
    Node* tmpPrev;
    Node* tmpNext;

    for(i = 0; i < p && tmp != nullptr; i++){
        tmp = tmp->getNext();
    }

    if(tmp == nullptr) return LinkedList::append(text);

    tmpPrev = tmp->getPrev();        tmpNext = tmp->getNext();

    Node* newNode = new Node(tmpPrev, text);
    newNode->setNext(tmp);

    tmp->setPrev(newNode);

    return 1;
}

int LinkedList::remove(int p){
    if(first == nullptr) return -1;

    int i;
    Node* tmp = first;
    Node* tmpPrev;
    Node* tmpNext;

    for(i = 0; i < p && tmp->getNext() != nullptr; i++){
        tmp = tmp->getNext();
    }

    tmpPrev = tmp->getPrev();        tmpNext = tmp->getNext();

    if(tmp == first) first = tmpNext;
    if(tmp == last) last = tmpPrev;

    if(tmpPrev != nullptr) tmpPrev->setNext(tmpNext);
    if(tmpNext != nullptr) tmpNext->setPrev(tmpPrev);

    delete tmp;

    return 1;
}

const char *LinkedList::get(int p){
    int i;
    Node* tmp = first;

    for(i = 0; i < p && tmp != nullptr; i++){
        tmp = tmp->getNext();
    }

    return tmp == nullptr ? nullptr : tmp->getText();
}

int LinkedList::indexOf(const char* text){
    int i;
    Node* tmp = first;

    for(i = 0; tmp->getText() != text && tmp->getNext() != nullptr; i++){
        tmp = tmp->getNext();
    }

    if(tmp->getText() == text)
        return i;
    else
        return -1;
}

const char *LinkedList::getFirst(){
    return first->getText();
}

const char *LinkedList::getLast(){
    return last->getText();
}

void LinkedList::visitAll(void (*work)(const char* text)){
    Node* tmp = first;
    int i;
    for(i = 0; tmp != nullptr; i++){
        (*work)(tmp->getText());
        tmp = tmp->getNext();
    }
}
```


----------



## VfL_Freak (24. Nov 2016)

Moin,

dazu liefert Google doch viel Hilfreiches, bspw. :
http://stackoverflow.com/questions/...s-argument-of-xxx-discards-qualifiers/5973485
https://www.c-plusplus.net/forum/272727-full
http://stackoverflow.com/questions/2382834/discards-qualifiers-error

Gruß Klaus


----------

