2012-07-26 33 views
0

我有一个问题(和抱歉,我的英语不好。)它代码块。迁移已成功完成,但默认的GNU编译器在编译时发现错误。VS到的cblock,性病::排序,</p> <p>我已经开发了VS 2008 Express的C++ C++程序,我想迁移操作<编译器错误

但是VS还没有找到任何东西。 我使用std::sort算法到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 ===| 

任何人有一个想法,我怎么能解决呢?

谢谢你的回答。

+0

请显示您称为'std :: sort()'的行。 – timrau 2012-07-27 00:23:54

+0

'尺寸 2012-07-27 00:27:19

+0

我也会为'operator <'const,即'operator <(const Item&theOther)'做参数。你是否像这样调用'sort'? 'std :: sort(vec.begin(),vec.end());' – 2012-07-27 00:31:05

回答

0

变化

bool operator<(Item& theOther)const 

bool operator<(const Item& theOther)const 

应该工作。

+0

谢谢。它工作正常。但是,VS如何在没有const的情况下使用它? – deeagle 2012-07-28 12:21:57