error: assignment of data-member ... in read-only structure

const double * OsiCsdpSolverInterface::getObjCoefficients() const
{
if (objCoeff != NULL)
delete[] objCoeff;

objCoeff = new double[nrcols];

return objCoeff;
}


I cut out everything that is not important. I am wrighting an interface, so this is one of lots of small methods where I need objCoeff. Therefor I initialized in the private part of the header of my class OsiCsdpSolverInterface the pointer:
double *objCoeff

in the Constructor I have set this Pointer equal to NULL so that there will never be any rubish stored in my array.
The idea is now: if the methode OsiCsdpSolverInterface::getObjCoefficients() is called, I first delete the old informations of objCoeff because they are no longer important and then I update the new information to return this array.

But the compiler says: error:
assignment of data-member 'OsiCsdpSolverInterface::objCoeff' in read-only structure

at line objCoeff = new double[nrcols];

Can anybody handle this problem? Thanks a lot
the const a the end of first line means that no members can be modified. Remove it.
@hamsterman

Thanks a lot, the compiler doesn´t call any problems. One last question concerning my comprehension of that problem.

In the overclass OsiSolverInterface, there are already these methods implemented as virtual methods so that I have to rewright them for my own problem. There the method OsiCsdpSolverInterface::objCoeff is defined with the const.
So if I delete the const entry in my class as you said, should I also delete it from my header?
And the second question is, does the compiler recognize that it is still the same methode that is overwritten from the virtual one?
I'm not sure removing const is the right way to go here.....

So if I delete the const entry in my class as you said, should I also delete it from my header?


Yes.

void foo::bar()
and
void foo::bar() const
are two different functions.

And the second question is, does the compiler recognize that it is still the same methode that is overwritten from the virtual one?

Not if you have mismatching consts. As illustrated above, they are totally different functions in the eyes of the compiler.




As for your original problem... the whole point of the const keyword is to offer a "guarantee" that the function will not change the state of the object. This allows it to be called with const objects.

What's more, your function is a "getter", so it really doesn't make sense for it to change the state of the object... since all it's doing is getting something.

So my question is this:

- what does reallocating the buffer have to do with getting coefficients?

It sounds to me like you're just trying to do the wrong thing with this function.
Topic archived. No new replies allowed.