以下のスニペットを (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;
}
実際のコードでは、コメント化された 2 行のコメントが解除されますが、コメント化された例でもエラーが発生します。私は何を間違っていますか?