Olof
Olof

Reputation: 797

Template copy constructor gives errors

I try to create a copy constructor to a list. The list has all variables as private members. Is there something special about a constructor in a template that does so this doesn't work?

I get these errors:

   1>          consoleapplication1\consoleapplication1\circulardoubledirectedlist.h(52) : while compiling class template member function 'CircularDoubleDirectedList<int>::CircularDoubleDirectedList(const CircularDoubleDirectedList<int> &)'
    1>          consoleapplication1\consoleapplication1\testdeepcopyingoflist.cpp(25) : see reference to function template instantiation 'CircularDoubleDirectedList<int>::CircularDoubleDirectedList(const CircularDoubleDirectedList<int> &)' being compiled
    1>          consoleapplication1\consoleapplication1\testdeepcopyingoflist.cpp(24) : see reference to class template instantiation 'CircularDoubleDirectedList<int>' being compiled
    1>consoleapplication1\consoleapplication1\circulardoubledirectedlist.h(57): error C2039: 'dir' : is not a member of 'CircularDoubleDirectedList<int>'
    1>consoleapplication1\consoleapplication1\circulardoubledirectedlist.h(62): error C2662: 'void CircularDoubleDirectedList<int>::changeDirection(void)' : cannot convert 'this' pointer from 'const CircularDoubleDirectedList<int>' to 'CircularDoubleDirectedList<int> &'
    1>          Conversion loses qualifiers
    1>consoleapplication1\consoleapplication1\circulardoubledirectedlist.h(68): error C2662: 'void CircularDoubleDirectedList<int>::moveCurrent(void)' : cannot convert 'this' pointer from 'const CircularDoubleDirectedList<int>' to 'CircularDoubleDirectedList<int> &'
    1>          Conversion loses qualifiers
    1>consoleapplication1\consoleapplication1\circulardoubledirectedlist.h(71): error C2662: 'void CircularDoubleDirectedList<int>::changeDirection(void)' : cannot convert 'this' pointer from 'const CircularDoubleDirectedList<int>' to 'CircularDoubleDirectedList<int> &'
    1>          Conversion loses qualifiers

My Code:

template <typename T>
CircularDoubleDirectedList<T>::CircularDoubleDirectedList(const CircularDoubleDirectedList<T>& other){

    bool changeDir;

    if (other.getCurrentDirection == ICircularDoubleDirectedList::FORWARD){
        changeDir = false;
    }
    else{
        changeDir = true;
        other.changeDirection();
    }

    int size = other.size();
    for (int i = 0; i < size; i++){
        this->addAtCurrent(other.getElementAtCurrent());
        other.moveCurrent();
    }
    if (changeDir){
        other.changeDirection();
    }
    this->currentDirection = other.getCurrentDirection();

}

Upvotes: 0

Views: 144

Answers (2)

juanchopanza
juanchopanza

Reputation: 227400

Your copy constructor (rightly) has a const reference parameter. However, it looks like you're trying to call non-const members on the argument:

other.changeDirection(); // this looks like a non-const operation
....

other.moveCurrent();     // this looks like a non-const operation

etc. It doesn't make much sense for a copy constructor to modify the thing being copied from, but if you really need that then you need the parameter to be non-const reference.

Alternatively, if those member functions don't modify the object, make them const.

Note that this has nothing to do with templates.

Upvotes: 2

Mike Seymour
Mike Seymour

Reputation: 254461

The first error doesn't seem to come from this code: there's no use of the name dir in the code.

The remaining errors are because you're trying to call non-const member functions on other, which is const. Either rethink what the constructor should do (since modifying its argument makes it rather confusing), or remove const from the parameter to allow modification.

Upvotes: 1

Related Questions