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

标签 c++ stl

以下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/

相关文章:

java - 将 C++ 回调转换为 Java

c++ - 如何检索无序 map 的碰撞?

c++ - g++ 4.9.3 提示 friended ctor 对 .emplace_back() 是私有(private)的,但喜欢 .push_back()

C++ STL::inplace_merge 和 sort 有什么区别

c++ - 为什么在 STL 中允许未定义的行为?

c++ - 使用 libpcap 速度限制发送数据包

c++ - 使用arduino键盘编译错误 "Event listener"

c++ - 如何最有效地从重载运算符返回多个值? (C++)

c++ - C冒泡排序: Sorted array loses biggest data piece and is replaced by a memory address

c++ - 使用 STL 将大型 STL vector 写入文件的最快方法