【发布时间】:2013-06-23 04:17:04
【问题描述】:
我在我的代码中声明了以下内容
vector <const A> mylist;
我得到以下编译错误 -
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
但如果声明 -
vector <A> mylist;
我的代码编译了。
在这种情况下不允许使用 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;
}
【问题讨论】:
-
你用的是什么编译器?这对我来说很好。
-
您希望通过 const 项实现什么目标?
-
@petric - 我已经用 g++ (GCC) 3.4.3 编译了它
-
@underscore_d 使用 gcc 3.4.3 编译:不是 c++11。