Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/145.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 当类用作映射值时,复制构造函数不起作用_C++ - Fatal编程技术网

C++ 当类用作映射值时,复制构造函数不起作用

C++ 当类用作映射值时,复制构造函数不起作用,c++,C++,我找不到一个比直接把它贴在这里更简单的解释我的问题的方法了。 我有一个模板类,带有必要的赋值运算符、默认构造函数和公共副本构造函数。当我尝试在代码中使用该类时,会出现如下错误: #include<map> #include<vector> template<class T> class BufferContainer { public: BufferContainer& operator=(BufferContainer& othe

我找不到一个比直接把它贴在这里更简单的解释我的问题的方法了。 我有一个模板类,带有必要的赋值运算符、默认构造函数和公共副本构造函数。当我尝试在代码中使用该类时,会出现如下错误:

#include<map>
#include<vector>

template<class T>
class BufferContainer
{
public:

    BufferContainer& operator=(BufferContainer& other)
    {
        buffer = other.get();
        return *this;
    }

    BufferContainer( const BufferContainer& other ) :
        buffer( other.get() )
     {
     }

    BufferContainer(){
    }

    std::vector<T>& get() {
            return buffer;
    }

    void add(T value) {

        buffer.push_back(value);
    }

    std::vector<T> buffer;
};

int main()
{
    std::map<int, BufferContainer<int> > myMap;
    myMap[1].add(1);
    return 1;
}
错误是:

Practice $ g++ template.cpp 
template.cpp: In instantiation of ‘BufferContainer<T>::BufferContainer(const BufferContainer<T>&) [with T = int; BufferContainer<T> = BufferContainer<int>]’:
/usr/include/c++/4.7/bits/stl_pair.h:105:31:   required from ‘std::pair<_T1, _T2>::pair(const _T1&, const _T2&) [with _T1 = const int; _T2 = BufferContainer<int>]’
/usr/include/c++/4.7/bits/stl_map.h:458:11:   required from ‘std::map<_Key, _Tp, _Compare, _Alloc>::mapped_type& std::map<_Key, _Tp, _Compare, _Alloc>::operator[](const key_type&) [with _Key = int; _Tp = BufferContainer<int>; _Compare = std::less<int>; _Alloc = std::allocator<std::pair<const int, BufferContainer<int> > >; std::map<_Key, _Tp, _Compare, _Alloc>::mapped_type = BufferContainer<int>; std::map<_Key, _Tp, _Compare, _Alloc>::key_type = int]’
template.cpp:38:9:   required from here
template.cpp:16:26: error: passing ‘const BufferContainer<int>’ as ‘this’ argument of ‘std::vector<T>& BufferContainer<T>::get() [with T = int]’ discards qualifiers [-fpermissive]
如果您能帮助我解决这个问题,我将不胜感激,更重要的是,请告诉我为什么会出现这个错误。
谢谢

这意味着您应该提供get方法的常量版本:

这意味着您只能在其他服务器上调用const方法


注意,返回类型是常量版本的get,是常量引用。这是常量正确性所必需的。

这意味着您应该提供get方法的常量版本:

这意味着您只能在其他服务器上调用const方法

注意,返回类型是常量版本的get,是常量引用。这是常量正确性所必需的。

其他作为常量引用,get是非常量方法。您不能从常量引用调用非常量方法。

其他方法作为常量引用,get是非常量方法。您不能从常量引用调用非常量方法。

您的get函数不是const限定的,您是通过复制构造函数中对const的引用来调用它的:

BufferContainer( const BufferContainer& other ) :
//               ^^^^^
    buffer( other.get() )
//          ^^^^^^^^^^^
{
}
这就是编译器抱怨的原因。不能通过对常量的引用调用非常量函数。引用const意味着您不打算修改被引用对象的状态,因此只能调用承诺不修改对象状态的函数。这就是成员函数上的const限定符的作用——实现这一承诺

因此,您的成员函数get应限定为const,并返回对const向量的引用:

如果您需要您的非const函数get,因为您希望允许客户端修改内部缓冲区建议:考虑这是否是一个好主意,那么您必须提供两个重载GET:

一个常量限定的函数,它返回对常量向量的引用,如上图所示 一种非常量限定的方法,返回对可修改向量(如原始get)的引用 get函数不是const限定函数,您是通过复制构造函数中对const的引用来调用它的:

BufferContainer( const BufferContainer& other ) :
//               ^^^^^
    buffer( other.get() )
//          ^^^^^^^^^^^
{
}
这就是编译器抱怨的原因。不能通过对常量的引用调用非常量函数。引用const意味着您不打算修改被引用对象的状态,因此只能调用承诺不修改对象状态的函数。这就是成员函数上的const限定符的作用——实现这一承诺

因此,您的成员函数get应限定为const,并返回对const向量的引用:

如果您需要您的非const函数get,因为您希望允许客户端修改内部缓冲区建议:考虑这是否是一个好主意,那么您必须提供两个重载GET:

一个常量限定的函数,它返回对常量向量的引用,如上图所示 一种非常量限定的方法,返回对可修改向量(如原始get)的引用
juanchopanza,谢谢。但这意味着我将无法再修改.get的结果。这是真的吗?@rahman:你可以而且应该提供两个版本的get,一个是像juanchopanza的常量,另一个是像你的一样的非常量。@rahman我编辑了我的问题,以明确你可以有两个版本,一个是常量,一个是非常量。谢谢大家。所有的问题都很好。不知道哪一个是真的,谢谢。但这意味着我将无法再修改.get的结果。这是真的吗?@rahman:你可以而且应该提供两个版本的get,一个是像juanchopanza的常量,另一个是像你的一样的非常量。@rahman我编辑了我的问题,以明确你可以有两个版本,一个是常量,一个是非常量。谢谢大家。所有的问题都很好。不知道哪一个是正确的。
BufferContainer( const BufferContainer& other ) :
//               ^^^^^
    buffer( other.get() )
//          ^^^^^^^^^^^
{
}
std::vector<T> const& get() const 
//             ^^^^^        ^^^^^
{
    return buffer;
}