简体   繁体   English

非静态 const 成员,不能使用默认赋值运算符

[英]Non-static const member, can't use default assignment operator

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 成员,'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 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.它将std::Pair<Ptr<double,double>, WeightValue*>放到一个向量上,其中WeightValue*是来自大约 3 个函数的 const 变量,而Ptr<double,double>取自一个迭代器在另一个向量上工作。

For future reference, Ptr<double,double> is a pointer to a Node object.为了将来参考, Ptr<double,double>是指向Node object 的指针。

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).现在,您在某些需要可分配sample的上下文中使用它 - 可能在容器中(如 map、向量或其他东西)。 This will fail, because the implicitly defined copy assignment operator does something along this line:这将失败,因为隐式定义的复制赋值运算符会沿着这条线做了一些事情:

// 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 not define one implicitly.但是a是 const .. 你必须使它成为非 const,它不会受到伤害,因为只要你不改变它:它在逻辑上仍然是 const :) 你也可以通过引入一个合适的operator=来解决这个问题,使编译器隐式定义一个。 But that's bad because you will not be able to change your const member.但这很糟糕,因为您将无法更改 const 成员。 Thus, having an operator=, but still not assignable: (because the copy and the assigned value are not identical!):因此,有一个 operator=,但仍然不能赋值:(因为副本和赋值不相同!):

struct sample {
    int const a; // const!

    sample(int a):a(a) { }

    // bad!
    sample & operator=(sample const&) { }
};

However in your case, the apparent problem apparently lies within std::pair<A, B> .但是,在您的情况下,明显的问题显然在于std::pair<A, B> Remember that a std::map is sorted on the keys it contains.请记住, std::map是按其包含的键排序的。 Because of that, you cannot change its keys, because that could easily render the state of a map invalid.因此,您无法更改其密钥,因为这很容易导致 map 的 state 无效。 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);

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! map 会向您抛出错误,因为在 map 中存储的某些值对中, ::first成员具有 const 限定类型!

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.请注意,这不是 GNU 特定的问题。 The ISO C++ standard requires that T has an assignment operator (see section 23.2.4.3). ISO C++ 标准要求 T 具有赋值运算符(参见第 23.2.4.3 节)。 I just showed on the example of GNU's STL implementation where this can lead to.我刚刚展示了 GNU 的 STL 实现的示例,这可能会导致。

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,因此您不能分配给它们。

At least mention which object the compiler is complaining about.至少要提到编译器抱怨的是哪个 object。 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.如果您没有,则使用默认值。可能,您在 class 中也有一个 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 ,因此我建议您将其更改为static const如果有意义的话。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 &#39;类型非静态const成员不能使用默认赋值运算符&#39; - 这是什么意思? - 'Type non-static const member can't use default assignment operator' - what does this mean? '非静态引用成员,不能使用默认赋值运算符' - 'non-static reference member, can't use default assignment operator' 错误:非静态引用成员 - 不能使用默认赋值运算符 - error: non-static reference member - can't use default assignment operator 错误:非静态引用成员,不能使用默认赋值运算符 - Error: non-static reference member, can't use default assignment operator 非静态参考成员&#39;int&Property <int> :: value&#39;,不能使用默认赋值运算符 - non-static reference member ‘int& Property<int>::value’, can’t use default assignment operator 错误:非静态引用成员&#39;std :: ostream&Student :: out&#39;,不能使用默认赋值运算符 - error: non-static reference member 'std::ostream& Student::out', can't use default assignment operator 具有非静态lambda成员的类不能使用默认模板参数? - Class with non-static lambda member can't use default template paramers? 默认参数:在非静态成员函数外部无效使用“ this” - default argument : invalid use of 'this' outside of a non-static member function 无效使用非静态成员,而成员是const且是列表初始化的吗? - Invalid use of non-static member where member is const and list-initialised? 使用 const_cast 在类中分配非静态常量成员 - assign non-static const member in class with const_cast
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM