C++ 非静态 const 成员,不能使用默认赋值运算符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/634662/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Non-static const member, can't use default assignment operator
提问by Ed James
A program I'm expanding uses std::pair<>
a lot.
我正在扩展的程序使用std::pair<>
很多。
There is a point in my code at which the compiler throws a rather large:
在我的代码中有一点,编译器会抛出一个相当大的:
Non-static const member, 'const Ptr std::pair, const double*>::first' can't use default assignment operator
非静态常量成员,'const Ptr std::pair, const double*>::first' 不能使用默认赋值运算符
I'm not really sure what this is referring to? Which methods are missing from the Ptr class?
我不太确定这是指什么?Ptr 类中缺少哪些方法?
The original call that causes this problem is as follows:
导致这个问题的原始调用如下:
vector_of_connections.pushback(pair(Ptr<double,double>,WeightValue*));
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.
它把 anstd::Pair<Ptr<double,double>, WeightValue*>
放到一个向量上, whereWeightValue*
是一个来自大约 3 个函数的 const 变量,并且Ptr<double,double>
从一个迭代器中获取,该迭代器在另一个向量上工作。
For future reference, Ptr<double,double>
is a pointer to a Node
object.
供以后参考,Ptr<double,double>
是一个指向Node
对象的指针。
回答by Johannes Schaub - litb
You have a case like this:
你有这样一个案例:
struct sample {
int const a; // const!
sample(int a):a(a) { }
};
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:
现在,您可以在某些需要sample
可分配的上下文中使用它- 可能在容器中(如地图、矢量或其他东西)。这将失败,因为隐式定义的复制赋值运算符沿着这条线做了一些事情:
// pseudo code, for illustration
a = other.a;
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 notdefine 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!):
但是a
是常量!。你必须使它成为非常量。它不会受到伤害,因为只要你不改变它,它在逻辑上仍然是 const :) 你可以通过引入一个合适的来解决这个问题operator=
,使编译器不会隐式定义一个。但这很糟糕,因为您将无法更改 const 成员。因此,有一个 operator=,但仍然不可分配!(因为副本和分配的值不相同!):
struct sample {
int const a; // const!
sample(int a):a(a) { }
// bad!
sample & operator=(sample const&) { }
};
Howeverin 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 cannotchange its keys, because that could easily render the state of a map invalid. Because of that, the following holds:
但是,在您的情况下,明显的问题显然在于std::pair<A, B>
. 请记住, astd::map
是按它包含的键排序的。因此,您无法更改其键,因为这很容易导致地图状态无效。因此,以下内容成立:
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);
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!
映射会向您抛出错误,因为在映射中存储的某个值对中,该::first
成员具有 const 限定类型!
回答by user288157
I faced the same issue, and came across this page.
我遇到了同样的问题,并遇到了这个页面。
http://blog.copton.net/archives/2007/10/13/stdvector/index.html
http://blog.copton.net/archives/2007/10/13/stdvector/index.html
From the page:
从页面:
Please note that this is no GNU specific problem here. The ISO C++ standard requires that T has an assignment operator (see section 23.2.4.3). I just showed on the example of GNU's STL implementation where this can lead to.
请注意,这不是 GNU 特定的问题。ISO C++ 标准要求 T 具有赋值运算符(请参阅第 23.2.4.3 节)。我刚刚在 GNU 的 STL 实现示例中展示了这可能导致的结果。
回答by James Curran
As far as I can tell, someplace you have something like:
据我所知,在某个地方,您有类似的东西:
// for ease of reading
typedef std::pair<const Ptr<double, double>, const double*> MyPair;
MyPair myPair = MAKEPAIR(.....);
myPair.first = .....;
Since the members of MyPair are const, you can't assign to them.
由于 MyPair 的成员是 const,您不能分配给他们。
回答by dirkgently
At least mention which object the compiler is complaining about. Most probably you are missing a custom assignment member. If you don't have one, the default one kicks in. Probably, you also have a const member in that class (whose objects are being assigned) and since a const member cannot be changed you hit that error.
至少提到编译器抱怨的是哪个对象。您很可能缺少自定义分配成员。如果您没有,则使用默认值。可能,您在该类中还有一个 const 成员(正在为其分配对象),并且由于无法更改 const 成员,因此您会遇到该错误。
Another approach: Since it's a class const
, I suggest that you change it to a static const
if that makes sense.
另一种方法:由于它是一个 class const
,我建议您将其更改为 astatic const
如果有意义。