C++ std::对向量
更新的解决方案:
对于我的特殊问题,排序调用位于标记为 const 的 Render 函数内。通过删除 const (不是我的偏好)或将排序放入另一个函数中(在本例中是我的更新的底部),问题就得到了解决。正如几个回答者所建议的那样,这是一个常量问题,只是不是我正在寻找的地方!
原始问题
我有一个指向这样声明的对象的指针的 std::vector 列表:
std::vector<Object*> myObjects;
我试图通过 getter 按成员数据对它们进行排序...我也有一个排序谓词。这是排序谓词,后跟 std::sort 调用:
bool SortByDistance(const Object* o1, const Object* o2)
{
return o1->GetDist() < o2->GetDist();
}
排序调用:
std::sort(myObjects.begin(), myObjects.end(), SortByDistance);
尽管抱怨只读位置或类似的分配,但我收到了十几个左右的错误:
/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
我确信我正在做的事情很愚蠢。 ..有人可以帮忙解释一下吗?我对 C++ 已经生疏了,现在又要重新开始了!任何建议将不胜感激。
更新
尽管尝试了iammilind的建议来完全删除const或确保const为100%(换句话说,我尝试将const添加到SortByDistance的末尾),但我仍然遇到相同的错误谓词,以及完全将其从这种情况中删除,
我认为其中一位评论者可能会建议我做一些愚蠢而危险的事情:将原始指针存储在我从未做过的STL容器中。不过,这样做有什么不同...将对象存储在未在堆上动态分配的容器中的原因是什么?我认为如果我不处理原始指针,我的排序问题的很多复杂性就会消失 我
我目前正在像这样创建我的对象:
std::vector<Object*> myObjects;
Object* tempObject = new Object;
myObjects.push_back(tempObject);
当然会在程序完成时释放该内存,但听起来这通常是一个坏主意
? stl_algo.h 中抱怨的代码:
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);
}
}
尽管尝试各种 const/no-const 解决方案,我仍然遇到相同的错误。如果我注释掉正在运行排序的行: std::sort(myObjects.begin(), myObjects.end(), SortByDistance); 当然,错误就会消失。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
仅从您的代码来看,我猜您的
Object::GetDist()
方法不是const
。在 C++ 中,class
的const
对象只能调用const
成员。如果是这种情况,那么您有两种方法可以消除此错误。
(1) 将
GetDist
设为const
:(2) 更改
SortByDistance
:Just from your code, I guess that your
Object::GetDist()
method is notconst
. In C++, aconst
object of aclass
can call onlyconst
members.If this is the case then you have 2 ways to remove this error.
(1) Make
GetDist
asconst
:(2) Change
SortByDistance
: