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

发布于 2024-07-14 10:43:27 字数 678 浏览 4 评论 0 原文

我正在扩展的程序经常使用 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

灰色世界里的红玫瑰 2024-07-21 10:43:27

您有这样的情况:

struct sample {
    int const a; // const!

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

现在,您在某些需要可分配 sample 的上下文中使用它 - 可能在容器中(如地图、矢量或其他东西)。 这将会失败,因为隐式定义的复制赋值运算符会执行以下操作:

// pseudo code, for illustration
a = other.a;

但是 a 是 const!。 你必须让它变得非常量。 这并没有什么坏处,因为只要你不改变它,它在逻辑上仍然是 const :) 你也可以通过引入合适的 operator= 来解决问题,使编译器不< /em> 隐式定义一个。 但这很糟糕,因为您将无法更改您的常量成员。 因此,有一个运算符=,但仍然不可分配! (因为副本和分配的值不相同!):

struct sample {
    int const a; // const!

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

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

但是在您的情况下,明显的问题显然在于std::pair。 请记住,std::map 是根据它包含的键进行排序的。 因此,您无法更改其键,因为这很容易使地图的状态无效。 因此,以下内容成立:

typedef std::map<A, B> map;
map::value_type <=> std::pair<A const, B>

也就是说,它禁止更改其包含的密钥! 因此,如果您这样做,

*mymap.begin() = make_pair(anotherKey, anotherValue);

映射会向您抛出错误,因为在映射中存储的某些值对中, ::first 成员具有 const 限定类型!

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:

// 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. 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&) { }
};

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);

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!

所谓喜欢 2024-07-21 10:43:27

我遇到了同样的问题,并遇到了这个页面。

http://blog.copton.net/archives/2007/10/13/stdvector/ index.html

从页面:

请注意,这不是 GNU 特有的问题。 ISO C++ 标准要求 T 具有赋值运算符(请参阅第 23.2.4.3 节)。 我刚刚展示了 GNU 的 STL 实现的示例,这可能会导致什么结果。

I faced the same issue, and came across this page.

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.

寒江雪… 2024-07-21 10:43:27

据我所知,在某个地方你有这样的情况:

// for ease of reading 
typedef std::pair<const Ptr<double, double>, const double*> MyPair;

MyPair myPair = MAKEPAIR(.....);
myPair.first = .....;

由于 MyPair 的成员是 const,所以你不能分配给他们。

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.

束缚m 2024-07-21 10:43:27

至少提及编译器抱怨的是哪个对象。 您很可能缺少自定义分配成员。 如果没有,则使用默认成员。可能该类中还有一个 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 a static const if that makes sense.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文