我正在扩展的程序经常使用 std::pair
。
在我的代码中有一个点,编译器会抛出一个相当大的错误:
非静态 const 成员,'const Ptr std::pair, const double*>::first' 不能使用默认赋值运算符
我不太确定这指的是什么?
Ptr 类中缺少哪些方法?
导致此问题的原始调用如下:
vector_of_connections.pushback(pair(Ptr<double,double>,WeightValue*));
将 std::Pair, WeightValue*>
放到向量上,其中 WeightValue* 是大约 3 个函数后面的 const 变量,而 Ptr
取自作用于另一个向量的迭代器。
为了将来参考,Ptr
是指向 Node
对象的指针。
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:
Non-static const member, 'const Ptr std::pair, const double*>::first' can't use default assignment operator
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*));
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.
For future reference, Ptr<double,double>
is a pointer to a Node
object.
发布评论
评论(4)
您有这样的情况:
现在,您在某些需要可分配
sample
的上下文中使用它 - 可能在容器中(如地图、矢量或其他东西)。 这将会失败,因为隐式定义的复制赋值运算符会执行以下操作:但是
a
是 const!。 你必须让它变得非常量。 这并没有什么坏处,因为只要你不改变它,它在逻辑上仍然是 const :) 你也可以通过引入合适的operator=
来解决问题,使编译器不< /em> 隐式定义一个。 但这很糟糕,因为您将无法更改您的常量成员。 因此,有一个运算符=,但仍然不可分配! (因为副本和分配的值不相同!):但是在您的情况下,明显的问题显然在于
std::pair
。 请记住,std::map
是根据它包含的键进行排序的。 因此,您无法更改其键,因为这很容易使地图的状态无效。 因此,以下内容成立:也就是说,它禁止更改其包含的密钥! 因此,如果您这样做,
映射会向您抛出错误,因为在映射中存储的某些值对中,
::first
成员具有 const 限定类型!You have a case like this:
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: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 suitableoperator=
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!):However in your case, the apparent problem apparently lies within
std::pair<A, B>
. Remember that astd::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:That is, it forbids changing its keys that it contains! So if you do
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!我遇到了同样的问题,并遇到了这个页面。
http://blog.copton.net/archives/2007/10/13/stdvector/ index.html
从页面:
I faced the same issue, and came across this page.
http://blog.copton.net/archives/2007/10/13/stdvector/index.html
From the page:
据我所知,在某个地方你有这样的情况:
由于 MyPair 的成员是 const,所以你不能分配给他们。
As far as I can tell, someplace you have something like:
Since the members of MyPair are const, you can't assign to them.
至少提及编译器抱怨的是哪个对象。 您很可能缺少自定义分配成员。 如果没有,则使用默认成员。可能该类中还有一个 const 成员(正在分配其对象),并且由于无法更改 const 成员,因此您会遇到该错误。
另一种方法:由于它是一个类
const
,我建议您将其更改为static const
(如果有意义的话)。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.
Another approach: Since it's a class
const
, I suggest that you change it to astatic const
if that makes sense.