C ++ - 排序算法没有看到我的重载“<”用户定义类型的运算符。

时间:2012-07-31 00:14:34

标签: c++ algorithm codeblocks

好的,我有一个名为fraction的用户定义类型,它代表具有分子和分母的普通分数。这是代码:

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;}
};

如您所见,它是在头文件中完全实现的。

所以我想要做的就是使用排序算法对分数进行排序。这是重载“<”的代码operator(注意:它与Fraction类放在同一个文件中,但在类之外):

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;
}

它的作用是检查分母是否相等,如果它们则测试分子的值。如果它们不相等,则使用最小公倍数和gcd来均衡它们。

当我想在main(编译时错误)中使用排序算法时,真正的问题就出现了。这是主要代码:

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;
}

我得到的错误是(注意:我已经正确地重载了​​分数的“&lt;&lt;”运算符,但不想在这里混淆):

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

2 个答案:

答案 0 :(得分:5)

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

问题是你的operator<重载只接受非const参数,但算法试图将Fraction的常量引用与容器中的元素进行比较。

由于operator<不会修改比较对象的内容,因此应该通过const引用来获取它们:

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

答案 1 :(得分:1)

考虑以下代码:

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

第二行基本上是对

的调用
bool res = a.operator<(b);
  1. 正如上面所建议的那样 - 从长远来看,const是正确的,
  2. 定义一个成员bool operator<(const Fraction&) const以使排序实现满意或者只是按照上面的第一个建议 - 与bool operator<(const Fraction& a, const Fraction& b)
  3. 一致