如何修复错误C2679:binary'>>' :找不到哪个操作员

时间:2014-03-13 02:48:01

标签: c++ oop

谁能帮我找错? 如何修复它们? 错误4错误C2264:'PhanSo :: Cong':函数定义或声明中的错误;功能未被称为

  

错误2错误C2679:二进制'>>' :找不到带有'const char [3]'

类型右手操作数的运算符
#include <conio.h>
#include <iomanip>
#include <iostream>
using namespace std;

class PhanSo
{
private:
    int xtu;
    int xmau;
public:
    void nhap();
    void xuat();
    PhanSo Cong(const PhanSo &);
    PhanSo Tru(const PhanSo &);
    PhanSo Nhan(const PhanSo &);
    PhanSo Chia(const PhanSo &);
};

void PhanSo::nhap()
{
    cout<<"Nhap tu so: ";
    cin>>"%d">>xtu;
    cout<<"Nhap mau so: ";
    cin>>"%d">>xmau;
}
void PhanSo::xuat()
{
    cout<<"\n %d/%d"<<xtu<<xmau;
}

int UCLN(int A, int B)
{
    int a = abs(A);
    int b = abs(B);
    while(a != b)
    {
        if(a > b)
            A = a - b;
        else
            B = b - a;
    }
    return A;

}
PhanSo PhanSo::Cong(const PhanSo &x)
{
    PhanSo tmp;
    tmp.xtu = xtu*x.xmau + xmau*x.xtu;
    tmp.xmau = xmau*x.xmau;

    int u = UCLN(tmp.xtu,tmp.xmau);
    tmp.xtu /=u;
    tmp.xmau /=u;
    return tmp;
}

PhanSo PhanSo::Cong(const PhanSo &x)
{
    PhanSo tmp;
    tmp.xtu = xtu*x.xmau - xmau*x.xtu;
    tmp.xmau = xmau*x.xmau;

    int u = UCLN(tmp.xtu,tmp.xmau);
    tmp.xtu /=u;
    tmp.xmau /=u;
    return tmp;
}

PhanSo PhanSo::Nhan(const PhanSo &x)
{
    PhanSo tmp;
    tmp.xtu = xtu*x.xtu;
    tmp.xmau = xmau*x.xmau;

    int u = UCLN(tmp.xtu,tmp.xmau);
    tmp.xtu /=u;
    tmp.xmau /=u;
    return tmp;
}

PhanSo PhanSo::Chia(const PhanSo &x)
{
    PhanSo tmp;
    tmp.xtu = xtu*x.xmau;
    tmp.xmau = xmau*x.xtu;

    int u = UCLN(tmp.xtu,tmp.xmau);
    tmp.xtu /=u;
    tmp.xmau /=u;
    return tmp;
}

void main()
{
    PhanSo a,b,kq;
    cout<<"Nhap phan so thu 1";
    a.nhap();
    cout<<"Nhap phan so thu 2";
    b.nhap();
    int key;
    cout<<setw(50)<<"\nMenu"<<endl;
    cout<<setw(50)<<"\n1. Tinh Tong";
    cout<<setw(50)<<"\n2. Tinh Hieu";
    cout<<setw(50)<<"\n3. Tinh Tich";
    cout<<setw(50)<<"\n4. Tinh Thuong";
    cout<<setw(50)<<"\n5. Thoat";
    cout<<setw(50)<<"\nBan chon:";
    cin>>key;
    switch (key)
    {
    case 1:
        kq = a.Cong(b);
        kq.xuat();
        break;
    case 2:
        kq = a.Tru(b);
        kq.xuat();
        break;
    case 3:
        kq = a.Nhan(b);
        kq.xuat();
        break;
    case 4:
        kq = a.Chia(b);
        kq.xuat();
        break;
    case 5:
        exit(0);
        break;
    default: cout<<"nhap sai.\n";
        break;
    }

}

1 个答案:

答案 0 :(得分:1)

将功能nhapxuat更改为:

void PhanSo::nhap()
{
    cout<<"Nhap tu so: ";
    cin>>xtu;
    cout<<"Nhap mau so: ";
    cin>>xmau;
}
void PhanSo::xuat() 
{
    cout<<"\n "<<xtu<< "/" <<xmau;
}

使用cin时不需要格式化字符串,就像使用scanf/fscanf时一样。