I'm reading Effective C++ (Scott Meyers), and getting the error " no match for operator* " when trying to compile following code from this book:
rational.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;}
};
rational.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() );
}
main.cpp
#include "rational.h"
int main()
{
rational r1(1,2);
rational r2;
r2 = 2*r1;
r2 = r1*3;
return 0;
}
Can someone explain why this is happening ?
See Question&Answers more detail:os