Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5997127
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T00:13:07+00:00 2026-05-23T00:13:07+00:00

UPDATED SOLUTION: With my particular problem, the sort call was inside a Render function

  • 0

UPDATED SOLUTION:

With my particular problem, the sort call was inside a Render function marked as const. By either removing the const (not my preference) or putting the sort in another function (in this case, the bottom of my Update), the problem is taken care of. As several answerer’s suggested, it WAS a const problem, just not where I was looking!


ORIGINAL PROBLEM

I have an std::vector list of pointers to objects declared thus:

std::vector<Object*> myObjects;

I’m trying to sort them by a member data through a getter… I have a sort predicate as well. Here’s the sort predicate followed by the std::sort call:

bool SortByDistance(const Object* o1, const Object* o2)
{
    return o1->GetDist() < o2->GetDist();
}

Sort call:

std::sort(myObjects.begin(), myObjects.end(), SortByDistance);

I’m getting a dozen or so errors though complaining about assignment of read-only location or similar:

/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS5.0.sdk/usr/include/c++/4.2.1/bits/stl_algo.h:2385: error: assignment of read-only location

I’m sure it is something silly I’m doing… could someone help shed some light? I’m rusty with my C++ and just getting back into it! Any suggestions would be much appreciated.

UPDATE

I’m still getting the same error, despite trying iammilind’s suggestions to remove the const altogether or ensure that const is 100% (in other words, I’ve tried adding const to the end of the SortByDistance predicate, as well as removing it from the situation altogether.

I’m thinking one of the commenters may be onto something by suggestion I am doing something stupid and dangerous: storing raw pointers to objects in STL containers. I’ve never done this any differently though… what are the reasons for storing objects in containers that aren’t dynamically allocated on the heap? I assume if I’m not dealing with raw pointers, a lot of the complexity of my sort problem goes away.

I’m currently creating my Objects like this:

std::vector<Object*> myObjects;
Object* tempObject = new Object;
myObjects.push_back(tempObject);

I of course release that memory when the program finishes, but it sounds like this is just a bad idea in general?

On a more related note (to the question I asked), here’s the code in stl_algo.h that is complaining:

  template<typename _RandomAccessIterator, typename _Compare>
void
__insertion_sort(_RandomAccessIterator __first,
         _RandomAccessIterator __last, _Compare __comp)
{
  if (__first == __last) return;

  for (_RandomAccessIterator __i = __first + 1; __i != __last; ++__i)
{
  typename iterator_traits<_RandomAccessIterator>::value_type
    __val = *__i;
  if (__comp(__val, *__first))
    {
      std::copy_backward(__first, __i, __i + 1);
      *__first = __val;  // this line is the one complaining about read-only assignment
    }
  else
    std::__unguarded_linear_insert(__i, __val, __comp);
}
}

Despite trying various const/no-const solutions, I still get the same error. If I comment out the line that is running the sort:
std::sort(myObjects.begin(), myObjects.end(), SortByDistance);
the errors, of course, go away.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T00:13:08+00:00Added an answer on May 23, 2026 at 12:13 am

    Just from your code, I guess that your Object::GetDist() method is not const. In C++, a const object of a class can call only const members.
    If this is the case then you have 2 ways to remove this error.

    (1) Make GetDist as const:

    class Object {
      int GetDist () const;  // <-- add const
    };
    

    (2) Change SortByDistance:

    bool SortByDistance (Object*, Object*); // <-- remove const
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

( Updated a little ) I'm not very experienced with internationalization using PHP, it
Problem: I have two spreadsheets that each serve different purposes but contain one particular
Update: We are still using XP at work and I got my solution working,
Just updated build server with rc0 and surprise no chiron. Wondering how you package
Here is the updated question: the current query is doing something like: $sql1 =
We've just updated ASP.NET from Preview 3 to Preview 5 and we've run into
I've updated php.ini and moved php_mysql.dll as explained in steps 6 and 8 here.
I've updated my TortoiseSVN client and now I'm getting the error when trying to
I've just updated my ruby installation on my gentoo server to ruby 1.8.6 patchlevel
I'm new to version control, so I apologize if there is a well-known solution

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.