非静态const成员,不能使用默认赋值运算符 [英] Non-static const member, can't use default assignment operator
问题描述
我正在扩展的程序使用 std :: pair<>
很多。
A program I'm expanding uses std::pair<>
a lot.
在我的代码中,编译器抛出一个相当大的一点:
There is a point in my code at which the compiler throws a rather large:
非静态const成员'const Ptr std :: pair, const double *> :: first'不能使用默认赋值运算符
Non-static const member, 'const Ptr std::pair, const double*>::first' can't use default assignment operator
我不确定这是指什么?
Ptr类中缺少哪些方法?
I'm not really sure what this is referring to? Which methods are missing from the Ptr class?
导致此问题的原始调用如下:
The original call that causes this problem is as follows:
vector_of_connections.pushback(pair(Ptr<double,double>,WeightValue*));
std :: Pair< Ptr< double,double> ,WeightValue *>
到向量上,其中 WeightValue *
是来自约3个函数的const变量, Ptr< double,double>
取自在另一个向量上工作的迭代器。
Where it's putting an std::Pair<Ptr<double,double>, WeightValue*>
onto a vector, where WeightValue*
is a const variable from about 3 functions back, and the Ptr<double,double>
is taken from an iterator that works over another vector.
为了将来参考, ; double,double>
是指向 Node
对象的指针。
For future reference, Ptr<double,double>
is a pointer to a Node
object.
推荐答案
你有这样的情况:
struct sample {
int const a; // const!
sample(int a):a(a) { }
};
现在,在某些上下文中使用 sample
是可分配的 - 可能在一个容器(如地图,矢量或其他)。这将失败,因为隐式定义的副本赋值操作符沿着这一行执行某些操作:
Now, you use that in some context that requires sample
to be assignable - possible in a container (like a map, vector or something else). This will fail, because the implicitly defined copy assignment operator does something along this line:
// pseudo code, for illustration
a = other.a;
但 a
是const!你必须使它非常量。它不伤害,因为只要你不改变它,它仍然在逻辑上const :)你可以通过引入一个合适的 operator =
修复这个问题,使得编译器不是隐式定义一个。但是这很糟糕,因为你不能更改你的const成员。因此,有一个operator =,但仍然不可分配! (因为副本和赋值不相同!):
But a
is const!. You have to make it non-const. It doesn't hurt because as long as you don't change it, it's still logically const :) You could fix the problem by introducing a suitable operator=
too, making the compiler not define one implicitly. But that's bad because you will not be able to change your const member. Thus, having an operator=, but still not assignable! (because the copy and the assigned value are not identical!):
struct sample {
int const a; // const!
sample(int a):a(a) { }
// bad!
sample & operator=(sample const&) { }
};
不过,表面上的问题显然在 std :: pair< A,B>
。记住, std :: map
在它包含的键上排序。因此,您无法更改其键,因为这可能会轻易地使地图的状态无效。因此,以下条件成立:
However in your case, the apparent problem apparently lies within std::pair<A, B>
. Remember that a std::map
is sorted on the keys it contains. Because of that, you cannot change its keys, because that could easily render the state of a map invalid. Because of that, the following holds:
typedef std::map<A, B> map;
map::value_type <=> std::pair<A const, B>
也就是说,它禁止更改其包含的键!所以如果你做
That is, it forbids changing its keys that it contains! So if you do
*mymap.begin() = make_pair(anotherKey, anotherValue);
地图会在您引发错误,因为在地图中存储的一些值对中, :: first
成员具有const限定类型!
The map throws an error at you, because in the pair of some value stored in the map, the ::first
member has a const qualified type!
这篇关于非静态const成员,不能使用默认赋值运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!