943,543 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 591
  • C++ RSS
Sep 23rd, 2009
0

Urgent :decrement of read-only location

Expand Post »
Hi everyone,

Can anyone tells me what this error means:

error: decrement of read-only location.

thanks
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
aomran is offline Offline
23 posts
since Sep 2009
Sep 23rd, 2009
0

Re: Urgent :decrement of read-only location

My guess is that it means just what it says.

Do you suppose you could post relevant code or some sort of context?
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Sep 23rd, 2009
0

Re: Urgent :decrement of read-only location

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
aomran is offline Offline
23 posts
since Sep 2009
Sep 23rd, 2009
0

Re: Urgent :decrement of read-only location

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
c++ Syntax (Toggle Plain Text)
  1. const int X(40); // set X to 40
  2. X--; // set X to 39 not allowed.

Run time, you may have written:
c++ Syntax (Toggle Plain Text)
  1. int *Ptr(10); // intending *Ptr to point to a location 10 but
  2. // actually pointing it to location
  3. // error here:
  4. (*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.
Reputation Points: 732
Solved Threads: 134
Practically a Master Poster
StuXYZ is offline Offline
659 posts
since Nov 2008
Sep 23rd, 2009
0

Re: Urgent :decrement of read-only location

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:

C++ Syntax (Toggle Plain Text)
  1. int main()
  2. {
  3. const int myVar = 5;
  4. int* j = const_cast<int*>(&myVar);
  5. *j = *j - 1;
  6. cout << *j << ": as you can see it's been decremented to 4.\n";
  7.  
  8. return 0;
  9. }
Reputation Points: 105
Solved Threads: 25
Posting Whiz in Training
necrolin is offline Offline
223 posts
since Jun 2009
Sep 23rd, 2009
1

Re: Urgent :decrement of read-only location

Or maybe you have a const method and you try to decrement a member variable in this method.
Last edited by jencas; Sep 23rd, 2009 at 5:13 am.
Reputation Points: 395
Solved Threads: 71
Posting Whiz
jencas is offline Offline
362 posts
since Dec 2007
Sep 23rd, 2009
0

Re: Urgent :decrement of read-only location

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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
aomran is offline Offline
23 posts
since Sep 2009
Sep 23rd, 2009
0

Re: Urgent :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.
Reputation Points: 105
Solved Threads: 25
Posting Whiz in Training
necrolin is offline Offline
223 posts
since Jun 2009
Sep 24th, 2009
0

Re: Urgent :decrement of read-only location

thanks guys it works
Reputation Points: 10
Solved Threads: 0
Newbie Poster
aomran is offline Offline
23 posts
since Sep 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC