尝试编译下面的代码段(使用 g++)时出现以下错误:
error: invalid initialization of non-const reference of type
‘std::vector<pos,std::allocator<pos> >&’ from a temporary of
type ‘std::vector<pos, std::allocator<pos> >&
(*)(std::vector<pos, std::allocator<pos> >&)’
这是生成错误的代码:
struct pos{
int start;
int end;
int distance;
int size;
};
bool compare_pos(pos a, pos b)
{
if (a.distance != b.distance)
return (a.distance < b.distance);
else
return (a.size < b.size);
}
vector<pos> sort_matches(vector<pos>& matches)
{
//vector<pos> sorted_matches(matches);
vector<pos> sorted_matches();
//sort(sorted_matches.begin(), sorted_matches.end(), compare_pos);
return sort_matches;
}
真正的代码会取消注释两个注释行,但即使是注释示例也会给我错误。我究竟做错了什么?