gpt4 book ai didi

c++ - 命名空间中的 gcc 编译时错误排序 vector

转载 作者:行者123 更新时间:2023-11-28 06:35:04 25 4
gpt4 key购买 nike

以下MWE在 (Cygwin) gcc 4.8.3 上编译失败,但在 MSCV 2010 上编译

#include <vector>
#include <algorithm>

namespace Namespace
{
struct Bar
{
};

bool barComparator( Bar& bar1 , Bar& bar2 )
{
return true;
}

struct Foo
{
void doStuff()
{
std::vector<Bar> barVec;
std::sort( barVec.begin() , barVec.end() , barComparator );
}
};
}

int main()
{
};

gcc 错误信息

In file included from /usr/lib/gcc/i686-pc-cygwin/4.8.3/include/c++/algorithm:62:0,
from Test007.cpp:2:
/usr/lib/gcc/i686-pc-cygwin/4.8.3/include/c++/bits/stl_algo.h: In instantiation of ‘_RandomAccessIterator std::__unguarded_partition(_RandomAccessIterator, _RandomAccessIterator, const _Tp&, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<Namespace::Bar*, std::vector<Namespace::Bar> >; _Tp = Namespace::Bar; _Compare = bool (*)(Namespace::Bar&, Namespace::Bar&)]’:
/usr/lib/gcc/i686-pc-cygwin/4.8.3/include/c++/bits/stl_algo.h:2296:78: required from ‘_RandomAccessIterator std::__unguarded_partition_pivot(_RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<Namespace::Bar*, std::vector<Namespace::Bar> >; _Compare = bool (*)(Namespace::Bar&, Namespace::Bar&)]’
/usr/lib/gcc/i686-pc-cygwin/4.8.3/include/c++/bits/stl_algo.h:2337:62: required from ‘void std::__introsort_loop(_RandomAccessIterator, _RandomAccessIterator, _Size, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<Namespace::Bar*, std::vector<Namespace::Bar> >; _Size = int; _Compare = bool (*)(Namespace::Bar&, Namespace::Bar&)]’
/usr/lib/gcc/i686-pc-cygwin/4.8.3/include/c++/bits/stl_algo.h:5490:44: required from ‘void std::sort(_RAIter, _RAIter, _Compare) [with _RAIter = __gnu_cxx::__normal_iterator<Namespace::Bar*, std::vector<Namespace::Bar> >; _Compare = bool (*)(Namespace::Bar&, Namespace::Bar&)]’
Test007.cpp:20:61: required from here
/usr/lib/gcc/i686-pc-cygwin/4.8.3/include/c++/bits/stl_algo.h:2263:35: error: invalid initialization of reference of type ‘Namespace::Bar&’ from expression of type ‘const Namespace::Bar’
while (__comp(*__first, __pivot))
^
/usr/lib/gcc/i686-pc-cygwin/4.8.3/include/c++/bits/stl_algo.h:2266:34: error: invalid initialization of reference of type ‘Namespace::Bar&’ from expression of type ‘const Namespace::Bar’
while (__comp(__pivot, *__last))

如果能帮我解决这个问题,我将不胜感激。

最佳答案

std::sort reference表示比较器函数不需要 const & 引用参数,但仍然不能修改参数。

看起来你的编译器有点限制和要求
bool barComparator( const Bar& bar1 , const Bar& bar2 )

关于c++ - 命名空间中的 gcc 编译时错误排序 vector ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26900077/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com