运算符不匹配* [英] no match for operator*

查看:51
本文介绍了运算符不匹配*的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读有效的C ++(Scott Meyers),并尝试编译本书中的以下代码时收到错误操作员不匹配*":

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

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

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 ?

推荐答案

您尚未在头文件中声明 operator * ,因此它在 main.cpp中不可见.

You haven't declared the operator* in your header file, so it isn't visible in main.cpp.

这篇关于运算符不匹配*的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆