48
votes

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
What exactly are you doing with the code? I don't see an assignment.rjh
Doesn't tell us much. Mind posting a bit more?dirkgently
Can you post Ptr in particular?JaredPar
just put the literal code. you've pasted some pseudo code again. also paste the other iterator's type and where you get it from... do not try to annotate the code. You are likely to make the same mistake in it like in your code. But paste the real code.Johannes Schaub - litb
No can do, it's not mine to paste. I can give you a theoretical outline but that's about it without some legal dubiousness. Anyway, it's like 15 function calls, and all the classes used are custom, so there'd be a LOT of code on here.Ed James

4 Answers

82
votes

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!

14
votes

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.

2
votes

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.

-1
votes

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.