简体   繁体   English

模板副本构造函数给出错误

[英]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();

}

Your copy constructor (rightly) has a const reference parameter. 您的副本构造函数(正确)具有const引用参数。 However, it looks like you're trying to call non- const members on the argument: 但是,您似乎正在尝试在参数上调用非const成员:

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. 等等。对于复制构造函数而言,修改从中复制的内容没有多大意义,但是如果您确实需要,则需要将参数作为非const引用。

Alternatively, if those member functions don't modify the object, make them const . 或者,如果那些成员函数不修改对象,则将它们const

Note that this has nothing to do with templates. 请注意,这与模板无关。

The first error doesn't seem to come from this code: there's no use of the name dir in the code. 第一个错误似乎并非源于此代码:代码中未使用名称dir

The remaining errors are because you're trying to call non-const member functions on other , which is const . 剩下的错误是因为您试图在other上调用非const成员函数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. 要么重新考虑构造函数应该做什么(因为修改其参数使其变得相当混乱),要么从参数中删除const以允许修改。

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

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