Hi everyone,

Can anyone tells me what this error means:

error: decrement of read-only location.

thanks

I will post the relevant code shortly, but I need more explanation of what decrement of read only location. location of what and how it has been decremented.

well your compile should give a line number.....
What you are not saying is if the error is at run-time or at compile time.

Compile time:

you may have written something like this

const int X(40);       // set X to 40
X--;                          // set X to 39 not allowed.

Run time, you may have written:

int *Ptr(10);   // intending *Ptr to point to a location 10  but 
                      // actually pointing it to location 
// error here:
(*Ptr)--;         // Maybe you intended to write *Ptr--

Actually the last error is a little difficult to find, without using the debugger or a lot of print statements.

Like the others already stated this error comes from trying to manipulate a constant. This should make you wonder a few things including:

Why am I trying to manipulate a constant? It totally beats the point of making something constant if you're going to decrement it later.

If you have to decrement the constant then why not just remove the const and make it a normal variable?

However, if you really need to do this and there's some logical reason for it then there's always const_cast<>. Take a look at my example:

int main()
{
   const int myVar = 5;
   int* j = const_cast<int*>(&myVar);
   *j = *j - 1;
   cout << *j << ": as you can see it's been decremented to 4.\n";

   return 0;
}

Or maybe you have a const method and you try to decrement a member variable in this method.

Edited 7 Years Ago by jencas: n/a

Comments
Good one, I didn't think of that.

I actually got these two messages

dheap.h: In member function ‘void Dheap<Process>::deleteMin() const’:
dheap.h:191: error: decrement of read-only location

The "const" at the end of your first error line is your problem. The member function is declared constant so it won't allow you to make any changes to variables in the function.

This article has been dead for over six months. Start a new discussion instead.