Vanio Begic
Vanio Begic

Reputation: 623

C++ - sort algorithm doesnt see my overloaded "<" operator for user-defined type.

Ok so I have 1 user-defined type named fraction and it represents and ordinary fraction with a numerator and denominator. Here is the code:

class Fraction
{
private:
  int numerator;
  int denominator;

public:
  Fraction(int numer,int denom)
    :numerator(numer),denominator(denom){}

  int get_denom(){return denominator;}
  int get_numer(){return numerator;}
};

As you see it is completly implemented in a header file.

So what I want to do with this is sort a vector of fractions using the sort algorithm. Here is code of overloaded "<" operator (note: It is placed in same file as the Fraction class, but outside of the class):

bool operator<(Fraction& first,Fraction& second)
{
  if(first.get_denom() == second.get_denom())
  {
    return first.get_numer()<second.get_numer()?true:false;
  }
  int first_num=first.get_denom();
  int second_num=second.get_denom();
  int lcm=(first_num*second_num)/gcd(first_num,second_num);
  int first_new_numerator=(lcm/first_num)*first.get_numer();
  int second_new_numerator=(lcm/second_num)*second.get_numer();

  return first_new_numerator<second_new_numerator?true:false;
}

What it does is check if denominators are equal if they are then it tests the values of numerators. If they are not equal it equalizes them using least common multiple and gcd.

The real problem starts when I want to use the sort algorithm in main (compile time error).Here is the main code:

Fraction parse_fraction(string& input)
{

  stringstream fraction_string(input);
  int numer;
  fraction_string>>numer;
  char seperator;
  fraction_string>>seperator;
  int denom;
  fraction_string>>denom;

  return Fraction(numer,denom);
}


int main()
{
  vector<Fraction> fractions;
  string input;
  while(cin>>input)
  {
    if(input=="|") break;
    fractions.push_back(parse_fraction(input));
  }

  sort(fractions.begin(),fractions.end);

  for(int i=0;i<fractions.size();i++)
  {
    cout<<fractions[i];
  }

  return 0;
}

Errors I get are (Note:I have properly overloaded "<<" operator for fractions but dont want to make clutter here):

/usr/include/c++/4.6/bits/stl_algo.h||In function ‘_RandomAccessIterator std::__unguarded_partition(_RandomAccessIterator, _RandomAccessIterator, const _Tp&) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<Fraction*, std::vector<Fraction> >, _Tp = Fraction]’:|

/usr/include/c++/4.6/bits/stl_algo.h:2253|70|instantiated from ‘_RandomAccessIterator std::__unguarded_partition_pivot(_RandomAccessIterator, _RandomAccessIterator) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<Fraction*, std::vector<Fraction> >]’|

/usr/include/c++/4.6/bits/stl_algo.h:2284|54|instantiated from ‘void std::__introsort_loop(_RandomAccessIterator, _RandomAccessIterator, _Size) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<Fraction*, std::vector<Fraction> >, _Size = long int]’|

/usr/include/c++/4.6/bits/stl_algo.h:5330|4|instantiated from ‘void std::sort(_RAIter, _RAIter) [with _RAIter = __gnu_cxx::__normal_iterator<Fraction*, std::vector<Fraction> >]’|

/home/vanio/Desktop/workspace/C++/Ordner/main.cpp:35|43|instantiated from here|

/usr/include/c++/4.6/bits/stl_algo.h|2212|error: no match for ‘operator<’ in ‘__first.__gnu_cxx::__normal_iterator<_Iterator, _Container>::operator* [with _Iterator = Fraction*, _Container = std::vector<Fraction>, __gnu_cxx::__normal_iterator<_Iterator, _Container>::reference = Fraction&]() < __pivot’|

/usr/include/c++/4.6/bits/stl_algo.h|2212|note: candidates are:|

/usr/include/c++/4.6/bits/stl_pair.h|207|note: template<class _T1, class _T2> bool std::operator<(const std::pair<_T1, _T2>&, const std::pair<_T1, _T2>&)|

/usr/include/c++/4.6/bits/stl_iterator.h|291|note: template<class _Iterator> bool std::operator<(const std::reverse_iterator<_Iterator>&, const std::reverse_iterator<_Iterator>&)|

/usr/include/c++/4.6/bits/stl_iterator.h|341|note: template<class _IteratorL, class _IteratorR> bool std::operator<(const std::reverse_iterator<_IteratorL>&, const std::reverse_iterator<_IteratorR>&)|

/usr/include/c++/4.6/bits/basic_string.h|2510|note: template<class _CharT, class _Traits, class _Alloc> bool std::operator<(const std::basic_string<_CharT, _Traits, _Alloc>&, const std::basic_string<_CharT, _Traits, _Alloc>&)|

/usr/include/c++/4.6/bits/basic_string.h|2522|note: template<class _CharT, class _Traits, class _Alloc> bool std::operator<(const std::basic_string<_CharT, _Traits, _Alloc>&, const _CharT*)|

/usr/include/c++/4.6/bits/basic_string.h|2534|note: template<class _CharT, class _Traits, class _Alloc> bool std::operator<(const _CharT*, const std::basic_string<_CharT, _Traits, _Alloc>&)|

/usr/include/c++/4.6/bits/stl_vector.h|1290|note: template<class _Tp, class _Alloc> bool std::operator<(const std::vector<_Tp, _Alloc>&, const std::vector<_Tp, _Alloc>&)|

/home/vanio/Desktop/workspace/C++/Ordner/Fraction.h|39|note: bool operator<(Fraction&, Fraction&)|

/home/vanio/Desktop/workspace/C++/Ordner/Fraction.h|39|note:   no known conversion for argument 2 from ‘const Fraction’ to ‘Fraction&’|

/usr/include/c++/4.6/bits/stl_algo.h|2215|error: no match for ‘operator<’ in ‘__pivot < __last.__gnu_cxx::__normal_iterator<_Iterator, _Container>::operator* [with _Iterator = Fraction*, _Container = std::vector<Fraction>, __gnu_cxx::__normal_iterator<_Iterator, _Container>::reference = Fraction&]()’|

/usr/include/c++/4.6/bits/stl_algo.h|2215|note: candidates are:|

/usr/include/c++/4.6/bits/stl_pair.h|207|note: template<class _T1, class _T2> bool std::operator<(const std::pair<_T1, _T2>&, const std::pair<_T1, _T2>&)|

/usr/include/c++/4.6/bits/stl_iterator.h|291|note: template<class _Iterator> bool std::operator<(const std::reverse_iterator<_Iterator>&, const std::reverse_iterator<_Iterator>&)|

/usr/include/c++/4.6/bits/stl_iterator.h|341|note: template<class _IteratorL, class _IteratorR> bool std::operator<(const std::reverse_iterator<_IteratorL>&, const std::reverse_iterator<_IteratorR>&)|

/usr/include/c++/4.6/bits/basic_string.h|2510|note: template<class _CharT, class _Traits, class _Alloc> bool std::operator<(const std::basic_string<_CharT, _Traits, _Alloc>&, const std::basic_string<_CharT, _Traits, _Alloc>&)|

/usr/include/c++/4.6/bits/basic_string.h|2522|note: template<class _CharT, class _Traits, class _Alloc> bool std::operator<(const std::basic_string<_CharT, _Traits, _Alloc>&, const _CharT*)|

/usr/include/c++/4.6/bits/basic_string.h|2534|note: template<class _CharT, class _Traits, class _Alloc> bool std::operator<(const _CharT*, const std::basic_string<_CharT, _Traits, _Alloc>&)|

/usr/include/c++/4.6/bits/stl_vector.h|1290|note: template<class _Tp, class _Alloc> bool std::operator<(const std::vector<_Tp, _Alloc>&, const std::vector<_Tp, _Alloc>&)|

/home/vanio/Desktop/workspace/C++/Ordner/Fraction.h|39|note: bool operator<(Fraction&, Fraction&)|

/home/vanio/Desktop/workspace/C++/Ordner/Fraction.h|39|note:   no known conversion for argument 1 from ‘const Fraction’ to ‘Fraction&’|
||=== Build finished: 22 errors, 0 warnings ===|

Upvotes: 1

Views: 2319

Answers (2)

YePhIcK
YePhIcK

Reputation: 5856

Consider this code:

Fraction a, b;
bool res = (a < b);

The second line is essentially a call to

bool res = a.operator<(b);
  1. As suggested above - being const correct is going to serve you very well in a long run
  2. Define a member bool operator<(const Fraction&) const to make the sorting implementation happy or just go with the first suggestion above - be const-correct with bool operator<(const Fraction& a, const Fraction& b)

Upvotes: 1

bool operator<(Fraction& first,Fraction& second)

The problem is that your overload of operator< only accepts non-const arguments, but the algorithm is trying to compare a constant reference to a Fraction with the elements in your container.

Since operator< does not modify the contents of the compared objects, it should take them by const reference:

bool operator<(Fraction const & first,Fraction const & second)

Upvotes: 5

Related Questions