Search code examples
c++sortingoperatorscodeblocksvisual-studio-express

VS to CBlocks, std::sort, operator< compiler error


I have a question(And Sorry for my bad english.)

I have developed a c++ program in VS 2008 Express C++, and I want to migrate it to Code Blocks. The migration was successfully, but the default GNU compiler find a mistake in the compile.

However the VS have not found anything. I use std::sort algorithm to a std::vector, contains classes instances. And I make a bool operator<()const to it.

    class Item{
        //...
        double size;

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

Now the compiler say me that:

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 ===|

Anybody has an idea how can I solve it?

Thank you for all answer.


Solution

  • Change

    bool operator<( Item& theOther)const
    

    into

    bool operator<(const Item& theOther)const
    

    should work.