-2

每当我编译我的 prg 时都会遇到这个错误。

c:\mingw\bin\../lib/gcc/mingw32/4.6.2/include/c++/bits/stl_algo.h:5394:5: note: template<class _RAIter> void std::sort(_RAIter, _RAIter)
c:\mingw\bin\../lib/gcc/mingw32/4.6.2/include/c++/bits/stl_algo.h:5430:5: note: void std::sort(_RAIter, _RAIter, _Compare) [with _RAIter = __gnu_cxx::__normal_iterator<PointTwoD*, std::vector<PointTwoD> >, _Compare = bool (MissionPlan::*)(PointTwoD&, PointTwoD&)]
c:\mingw\bin\../lib/gcc/mingw32/4.6.2/include/c++/bits/stl_algo.h:5430:5: note:   no known conversion for argument 3 from '<unresolved overloaded function type>' to 'bool (MissionPlan::*)(PointTwoD&, PointTwoD&)'

下面实际上是我的 .cpp 文件..

bool MissionPlan::sortByCiv(const PointTwoD &t1, const PointTwoD &t2)
{
    return t1.locationdata.getCivIndex() < t2.locationdata.getCivIndex();
}

void MissionPlan::topfives()
{   

    topfive.assign( point1.begin(), point1.end() ); 

    sort(topfive.begin(), topfive.end(), sortByCiv);

    for(int i=0; i < 5; i++)
    {
        topfive.at(i).displayPointdata();
    }

}

任务计划.h

class MissionPlan
{

    private:
        int sizeofarray;
        int sizeofarray2;
        int xcordi;
        int ycordi;
        LocationData locationdata;
        PointTwoD pointtwoD;
        //MissionPlan missionplan;


    public:
        MissionPlan();
        MissionPlan(int, int, float);

        int getx();
        int gety();
        float civnum;
        float getciv();

        void stats();
        void storedata(int, int, float);
        void test();
        void displayall();
        void compute();
        void topfives();
        static bool sortByCiv(const PointTwoD &t1, const PointTwoD &t2);

};

当我从代码“sort(topfive.begin(), topfive.end(), sortByCiv);”中删除这一行时,我的程序将编译并顺利运行
那么那行代码有问题还是有其他影响它的东西?


这是我在创建 sortByCiv statc 并将函数 para 更改为 const 后开始编译时得到的错误消息。

In file included from c:\mingw\bin\../lib/gcc/mingw32/4.6.2/include/c++/algorithm:63:0,
                 from ..\src\/MissionPlan.h:9,
                 from ..\src\MissionPlanImp.cpp:3:
c:\mingw\bin\../lib/gcc/mingw32/4.6.2/include/c++/bits/stl_algo.h: In function '_RandomAccessIterator std::__unguarded_partition(_RandomAccessIterator, _RandomAccessIterator, const _Tp&, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<PointTwoD*, std::vector<PointTwoD> >, _Tp = PointTwoD, _Compare = bool (*)(PointTwoD&, PointTwoD&)]':
c:\mingw\bin\../lib/gcc/mingw32/4.6.2/include/c++/bits/stl_algo.h:2265:78:   instantiated from '_RandomAccessIterator std::__unguarded_partition_pivot(_RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<PointTwoD*, std::vector<PointTwoD> >, _Compare = bool (*)(PointTwoD&, PointTwoD&)]'
c:\mingw\bin\../lib/gcc/mingw32/4.6.2/include/c++/bits/stl_algo.h:2306:62:   instantiated from 'void std::__introsort_loop(_RandomAccessIterator, _RandomAccessIterator, _Size, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<PointTwoD*, std::vector<PointTwoD> >, _Size = int, _Compare = bool (*)(PointTwoD&, PointTwoD&)]'
c:\mingw\bin\../lib/gcc/mingw32/4.6.2/include/c++/bits/stl_algo.h:5445:4:   instantiated from 'void std::sort(_RAIter, _RAIter, _Compare) [with _RAIter = __gnu_cxx::__normal_iterator<PointTwoD*, std::vector<PointTwoD> >, _Compare = bool (*)(PointTwoD&, PointTwoD&)]'
..\src\MissionPlanImp.cpp:140:48:   instantiated from here
c:\mingw\bin\../lib/gcc/mingw32/4.6.2/include/c++/bits/stl_algo.h:2233:4: error: invalid initialization of reference of type 'PointTwoD&' from expression of type 'const PointTwoD'

c:\mingw\bin../lib/gcc/mingw32/4.6.2/include/c++/bits/stl_algo.h:2236:4:错误:从类型'const'的表达式中对类型'PointTwoD&'的引用无效初始化PointTwoD'

现在程序终于可以在命令提示符下运行,但它会因以下错误消息而终止。

"terminate called after throwing an instance of 'std::out_of_range' what(): vecotr::_M_range_check"

谢谢大家,我已经启动并运行了,感谢并感谢您的所有帮助..

4

2 回答 2

5

这里的问题是它是一个成员函数sortByCiv,它只能相对于某个接收器对象调用(即myObject.sortByCiv(...)sortByCiv(...).std::sort一个值。

要解决此问题,请制作sortByCiv static. 这使它不再具有接收器对象,应该可以解决您的问题。

希望这可以帮助!

于 2013-10-25T20:33:14.093 回答
0

sortByCiv 是一个成员函数,需要特殊处理;例如,您不能将其转换为函数指针类型,因为它具有隐式(第三个)this 参数。

在你的情况下,你不需要。只需将 sortByCiv(..) 移出类声明(或使其成为静态),因为它不使用参数:

bool sortByCiv(const PointTwoD &t1, const PointTwoD &t2)
{
    return t1.locationdata.getCivIndex() < t2.locationdata.getCivIndex();
}
于 2013-10-25T20:36:11.130 回答