与操作符不匹配+ c++ 中的错误

发布于 2024-12-08 23:35:12 字数 8339 浏览 0 评论 0原文

这是我一直在研究的 Rational 类:

rational.hrational.cpp

#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

#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 function ‘int main()’:
driver.cpp:6:12: error: no match for ‘operator+’ 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 for ‘operator/’ in ‘r1 / r3’

当我注释使用运算符+和运算符/的行时,代码就可以工作。 这让我感到困惑,因为我已经使用operator+实现了operator-,并且类似地使用operator*实现了operator/。 因此,如果其中一个有效,我认为另一个也会有效。 有人可以解释一下我在这里做错了什么吗?

编辑 当我使用运算符 == 时,我会收到更多错误

rational.cpp:22:6: error: prototype for ‘bool 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 for ‘bool 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 for ‘operator==’ 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>&)

这些神秘消息意味着什么?

Here is the Rational class that i've been working on:

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

Here's the error that i get when trying to compile it.

driver.cpp: In function ‘int main()’:
driver.cpp:6:12: error: no match for ‘operator+’ 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 for ‘operator/’ in ‘r1 / r3’

When I comment the lines where I use the operator+ and operator/, then the code works.
This baffles me as I have implemented the operator- using operator+ and similarly the operator/ using operator*.
So if one of them works, I'd figure that the other would work too.
Can someone please explain what I have done wrong here?

Edit
I get even more errors when I use the operator==

rational.cpp:22:6: error: prototype for ‘bool 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 for ‘bool 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 for ‘operator==’ 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>&)

What do these cryptic messages mean?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夏见 2024-12-15 23:35:12

r1 不是 Rational 类型的对象,正如您所声明的那样,它是一个不带参数并返回 Rational 的函数。

r1() + r2 将是一个有效的表达式,但您可能想让 r1 成为 Rational

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

请注意,您的 operator+< /code> 相对于 const 是不对称的。我通常建议将 operator+ 设为自由函数:

Rational operator+( const Rational&, const Rational& );

如果您希望它成为成员函数,则可能应该将其声明为 const

Rational operator+( const Rational& ) const;

r1 isn't an object of type Rational, as you've declared it, it's a function taking no parameters and returning a Rational.

r1() + r2 would be a valid expression, but you probably meant to make r1 a Rational:

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

Note that your operator+ is assymmetric w.r.t const. I normally recommend making operator+ a free function:

Rational operator+( const Rational&, const Rational& );

If you want it to be a member function it should probably be declared const:

Rational operator+( const Rational& ) const;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文