0

我有一个问题(对不起我的英语不好。)

我在 VS 2008 Express C++ 中开发了一个 c++ 程序,我想将它迁移到代码块。迁移成功,但默认的 GNU 编译器在编译时发现错误。

但是VS没有发现任何东西。我使用std::sort算法来 a std::vector,包含类实例。我对此表示bool operator<()const赞同。

    class Item{
        //...
        double size;

    public:
        bool operator<( Item& theOther)const{
            return size < theOther.size ? true : false;
        }
        //...
    }

现在编译器告诉我:

c:\mingw\bin\..\lib\gcc\mingw32\4.5.2\include\c++\bits\stl_algo.h||In function '_RandomAccessIterator std::__unguarded_partition(_RandomAccessIterator, _RandomAccessIterator, const _Tp&) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<Item*, std::vector<Item> >, _Tp = Item]':|

c:\mingw\bin\..\lib\gcc\mingw32\4.5.2\include\c++\bits\stl_algo.h:2249|70|instantiated from '_RandomAccessIterator std::__unguarded_partition_pivot(_RandomAccessIterator, _RandomAccessIterator) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<Item*, std::vector<Item> >]'|

c:\mingw\bin\..\lib\gcc\mingw32\4.5.2\include\c++\bits\stl_algo.h:2280|54|instantiated from 'void std::__introsort_loop(_RandomAccessIterator, _RandomAccessIterator, _Size) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<Item*, std::vector<Item> >, _Size = int]'|

c:\mingw\bin\..\lib\gcc\mingw32\4.5.2\include\c++\bits\stl_algo.h:5212|4|instantiated from 'void std::sort(_RAIter, _RAIter) [with _RAIter = __gnu_cxx::__normal_iterator<Item*, std::vector<Item> >]'|

D:\the_user\packing\packing\PackingProblem.h:426|41|instantiated from here|
c:\mingw\bin\..\lib\gcc\mingw32\4.5.2\include\c++\bits\stl_algo.h|2208|error: no match for 'operator<' in '__first.__gnu_cxx::__normal_iterator<_Iterator, _Container>::operator* [with _Iterator = Item*, _Container = std::vector<Item>, __gnu_cxx::__normal_iterator<_Iterator, _Container>::reference = Item&]() < __pivot'|

D:\the_user\packing\packing\Item.h|15|note: candidate is: bool Item::operator<(Item&) const|
||=== Build finished: 2 errors, 0 warnings ===|

有人知道我该如何解决吗?

谢谢大家的回答。

4

1 回答 1

0

改变

bool operator<( Item& theOther)const

进入

bool operator<(const Item& theOther)const

应该管用。

于 2012-07-27T14:39:03.967 回答