我正在阅读 Effective C++ (Scott Meyers),并在尝试编译本书中的以下代码时收到错误“no match for operator*”:
理性的.h
class rational
{
private:
int num;
int den;
public:
rational(int n = 0, int d = 1);
int getNum() const {return num;}
int getDen() const {return den;}
};
理性的.cpp
#include "rational.h"
rational::rational(int n,
int d)
:num(n),
den(d)
{}
const rational operator*(const rational &lhs,
const rational &rhs)
{
return rational( lhs.getNum()*rhs.getNum(),
lhs.getDen()*rhs.getDen() );
}
主文件
#include "rational.h"
int main()
{
rational r1(1,2);
rational r2;
r2 = 2*r1;
r2 = r1*3;
return 0;
}
有人可以解释为什么会这样吗?