C++中出现了“no match for operator+”错误。

6

这是我一直在工作的Rational类:

rational.h

#include<iostream>

using namespace std;

#ifndef RATIONAL_H
#define RATIONAL_H

class Rational
{
  int numerator,denominator;
  public:
  // the various constructors
  Rational();
  Rational(int);
  Rational(int,int);

  //member functions
  int get_numerator()const{return numerator;}
  int get_denominator()const{return denominator;}

  // overloaded operators
  // relational operators
  bool operator==(const Rational&)const;
  bool operator<(const Rational&)const;
  bool operator<=(const Rational&)const;
  bool operator>(const Rational&)const;
  bool operator>=(const Rational&)const;

  //arithmetic operators
  Rational operator+(const Rational&);
  Rational operator-(const Rational&);
  Rational operator*(const Rational&);
  Rational operator/(const Rational&);

  //output operator
  friend ostream& operator<<(ostream&, const Rational&);
};
#endif //RATIONAL_H

rational.cpp

#include "rational.h"

// implementation ofthe various constructors
Rational::Rational()
  :numerator(0),denominator(1){}

Rational::Rational(int number)
  :numerator(number),denominator(1){}

Rational::Rational(int n,int d)
  :numerator(n),denominator(d)
{
  if(denominator == 0) denominator = 1;
  if(denominator < 0)
  {
    numerator *= -1;
    denominator *= -1;
  }
}

// implementation of overloaded operators
bool Rational::operator==(const Rational& rhs)const
{
  if( numerator * rhs.get_denominator() == denominator * rhs.get_numerator() )
  {
    return true;
  }
  else return false;
}

bool Rational::operator<(const Rational& rhs)const
{
  if( numerator * rhs.get_denominator() < denominator * rhs.get_numerator() )
  {
    return true;
  }
  else return false;
}

bool Rational::operator<=(const Rational& rhs)const
{
  return operator==(rhs) || operator<(rhs);
}

bool Rational::operator>(const Rational& rhs)const
{
  return !operator<(rhs);
}

bool Rational::operator>=(const Rational& rhs)const
{
  return operator==(rhs) || operator>(rhs);
}

//arithmetic operators
Rational Rational::operator+(const Rational& rhs)
{
  return Rational( (numerator * rhs.get_denominator() + denominator*rhs.get_numerator()), (denominator * rhs.get_denominator()) );
}

Rational Rational::operator-(const Rational& rhs)
{
  //reuse of the + operator for substraction
  return operator+(Rational(-1*rhs.get_numerator(),rhs.get_denominator()));
}

Rational Rational::operator*(const Rational& rhs)
{
  return Rational(numerator * rhs.get_numerator(), denominator * rhs.get_denominator());
}

Rational Rational::operator/(const Rational& rhs)
{
  //reuse of the * operator as division is the inverse of multiplication
  return operator*(Rational(rhs.get_denominator(),rhs.get_numerator()));
}

// friend output operator
ostream& operator<<(ostream& os, const Rational& r)
{
   os<<r.get_numerator()<<"/"<<r.get_denominator();
   return os;
}

and the driver for the program driver.cpp
#include "rational.h"

int main()
{
  Rational r1(),r2(3),r3(11,3),tmp;
  cout<<r1+r2<<endl;
  cout<<r2<<endl;
  cout<<r2-r3<<endl;
  cout<<r2*r3<<endl;
  cout<<r1/r3;

  return 0;
}

当我尝试编译时,出现以下错误。

driver.cpp: In functionint main()’:
driver.cpp:6:12: error: no match foroperator+’ in ‘r1 + r2’
driver.cpp:6:12: note: candidates are:
/usr/include/c++/4.6/bits/stl_iterator.h:327:5: note: template<class _Iterator> std::reverse_iterator<_Iterator> std::operator+(typename std::reverse_iterator<_Iterator>::difference_type, const std::reverse_iterator<_Iterator>&)
/usr/include/c++/4.6/bits/basic_string.h:2306:5: note: template<class _CharT, class _Traits, class _Alloc> std::basic_string<_CharT, _Traits, _Alloc> std::operator+(const std::basic_string<_CharT, _Traits, _Alloc>&, const std::basic_string<_CharT, _Traits, _Alloc>&)
/usr/include/c++/4.6/bits/basic_string.tcc:694:5: note: template<class _CharT, class _Traits, class _Alloc> std::basic_string<_CharT, _Traits, _Alloc> std::operator+(const _CharT*, const std::basic_string<_CharT, _Traits, _Alloc>&)
/usr/include/c++/4.6/bits/basic_string.tcc:710:5: note: template<class _CharT, class _Traits, class _Alloc> std::basic_string<_CharT, _Traits, _Alloc> std::operator+(_CharT, const std::basic_string<_CharT, _Traits, _Alloc>&)
/usr/include/c++/4.6/bits/basic_string.h:2343:5: note: template<class _CharT, class _Traits, class _Alloc> std::basic_string<_CharT, _Traits, _Alloc> std::operator+(const std::basic_string<_CharT, _Traits, _Alloc>&, const _CharT*)
/usr/include/c++/4.6/bits/basic_string.h:2359:5: note: template<class _CharT, class _Traits, class _Alloc> std::basic_string<_CharT, _Traits, _Alloc> std::operator+(const std::basic_string<_CharT, _Traits, _Alloc>&, _CharT)
driver.cpp:10:12: error: no match foroperator/’ in ‘r1 / r3’

当我注释掉使用 operator+ 和 operator/ 的代码行时,代码就可以工作了。这让我感到困惑,因为我已经使用 operator+ 实现了 operator-,同样地,我也使用 operator* 实现了 operator/。所以如果其中一个可用,我认为另一个也应该可用。请问有人能够解释我在这里做错了什么吗?

编辑 当我使用 operator== 时,会出现更多的错误。

rational.cpp:22:6: error: prototype forbool Rational::operator==(const Rational&)’ does not match any in class ‘Rational’
rational.h:23:8: error: candidate is: bool Rational::operator==(Rational)
rational.cpp:31:6: error: prototype forbool Rational::operator<(const Rational&) const’ does not match any in class ‘Rational’
rational.h:24:8: error: candidate is: bool Rational::operator<(Rational)
driver.cpp: In function ‘int main()’:
driver.cpp:11:10: error: no match foroperator==’ in ‘r1 == tmp’
driver.cpp:11:10: note: candidates are:
/usr/include/c++/4.6/bits/postypes.h:218:5: note: template<class _StateT> bool std::operator==(const std::fpos<_StateT>&, const std::fpos<_StateT>&)
/usr/include/c++/4.6/bits/stl_pair.h:201:5: 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:285:5: 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:335:5: 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/allocator.h:122:5: note: template<class _T1, class _T2> bool std::operator==(const std::allocator<_T1>&, const std::allocator<_T2>&)
/usr/include/c++/4.6/bits/allocator.h:127:5: note: template<class _Tp> bool std::operator==(const std::allocator<_Tp1>&, const std::allocator<_Tp1>&)
/usr/include/c++/4.6/bits/basic_string.h:2427:5: 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:2434:5: note: template<class _CharT> typename __gnu_cxx::__enable_if<std::__is_char<_Tp>::__value, bool>::__type std::operator==(const std::basic_string<_CharT>&, const std::basic_string<_CharT>&)
/usr/include/c++/4.6/bits/basic_string.h:2448:5: 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/basic_string.h:2460:5: 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/streambuf_iterator.h:194:5: note: template<class _CharT, class _Traits> bool std::operator==(const std::istreambuf_iterator<_CharT, _Traits>&, const std::istreambuf_iterator<_CharT, _Traits>&)

这些神秘的消息是什么意思?

3
尝试在声明r1时去掉括号(即使用Rational r1,r2(3),...),而不是保留括号。 - Cameron
1
尽量不要使用using namespace,并且绝不要在全局范围内使用它,特别是在头文件中。 - Cat Plus Plus
@CatPlusPlus: 我正在学习C++,你可以解释一下为什么这是一个不好的做法吗? - nikhil
@nihkil:例如:https://dev59.com/gXM_5IYBdhLWcg3wslfs。此外,你应该考虑获取一本好的C++教材来学习。SO是一个获取特定问题答案的地方;它不能替代好书籍的学习。 - CB Bailey
@CharlesBailey: 我已经从Scott Meyers那里得到了Effective C++。我刚刚浏览了前几个条目,我正在尝试使使用const成为可能的习惯。 - nikhil
尊敬的您,鉴于您所提出的问题,虽然我认为《Effective C++》是一本很好的书,但它并不是针对您目前水平的读者群体。建议您查看这里,以获取更适合初学者的文献资料:https://dev59.com/_3RC5IYBdhLWcg3wK9yV。 - CB Bailey
1个回答

7

r1不是你声明的Rational类型的对象,而是一个不带参数并返回Rational的函数。

r1() + r2是有效的表达式,但你可能想把r1定义为Rational类型:

Rational r1,r2(3),r3(11,3),tmp;

请注意,您的operator+const方面是非对称的。我通常建议将operator+设置为自由函数:
Rational operator+( const Rational&, const Rational& );

如果你希望它成为成员函数,它应该声明为const
Rational operator+( const Rational& ) const;

哇,那个起作用了。 但是我们不能以这种方式调用默认构造函数吗?而且我不应该得到一个编译器错误,指出函数体缺失吗?另外,你如何调试这些神秘的消息? - nikhil
@nikhil:你不能“调用”构造函数。你定义对象,实现会选择适当的方式来初始化已声明的对象。如果你提供了一个用户定义的默认构造函数,它将被用于初始化那些没有初始化器的对象。你不需要做任何特殊的事情。 - CB Bailey
谢谢你对我的问题的帮助, 还有一个疑问,当你说一个自由函数时,你是指像 ostream 运算符这样的友元函数吗? - nikhil
@nikhil:不,我只是指一个非成员函数。它可以是友元,但最好不要这样。一种常见的习惯是将复合赋值运算符作为成员函数,并通过它们来实现对称二进制算术函数(+-等)的功能。这通常意味着它们不必成为友元。 - CB Bailey

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接