小编Sat*_*bdi的帖子

const对象传染媒介给编译错误

我在我的代码中声明了以下内容

vector <const A> mylist; 
Run Code Online (Sandbox Code Playgroud)

我得到以下编译错误 -

new_allocator.h:75: error: `const _Tp* __gnu_cxx::new_allocator<_Tp>::address(const _Tp&) const \[with _Tp = const A]' and `_Tp* __gnu_cxx::new_allocator<_Tp>::address(_Tp&) const [with _Tp = const A]' cannot be overloaded
Run Code Online (Sandbox Code Playgroud)

但如果宣布 -

vector <A> mylist;
Run Code Online (Sandbox Code Playgroud)

我的代码编译.

在这种情况下不允许使用const吗?

我在这里复制我的代码供大家参考 -

#include <iostream>
#include <vector>

using namespace std;
class A
{
public:
    A () {cout << "default constructor\n";}
    A (int i): m(i) {cout << "non-default constructor\n";}

private:
    int m;
};

int main (void)
{
    vector<const A> mylist;

    mylist.push_back(1);

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

c++ stdvector c++98

35
推荐指数
2
解决办法
1万
查看次数

标签 统计

c++ ×1

c++98 ×1

stdvector ×1