Hi everyone,

Can anyone tells me what this error means:

error: decrement of read-only location.

thanks

Recommended Answers

All 8 Replies

My guess is that it means just what it says.

Do you suppose you could post relevant code or some sort of context?

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;
}
Member Avatar for jencas

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

commented: Good one, I didn't think of that. +1

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.

thanks guys it works

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.