运营商重载问题。 (私人会员问题)

时间:2014-03-11 05:53:55

标签: c++ operator-overloading

我正在做一个关于运算符重载的教程,在编写本书中的代码时,我在下一节中遇到了错误:

std::ostream &operator<<(std::ostream &outst, const AC_current &c)
    {
        outst << std::setiosflags(std::ios::fixed)<<std::setprecision(2);
        outst << "(" << std::setw(6) << c.mag; //----->'mag' is a private member of 'AC_current'    
        outst << ", " << std::setw(6) << c.phase<<(char)248<< ") A"; //--->'phase' is a private member of 'AC_current'
        return outst;
    }

我将它定义为朋友功能,但它不起作用。我做错了什么?

P.S.这是完整的代码。

#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
const double Deg2Rad = 2*M_PI/360;
const double Rad2Deg = 360/(2*M_PI);

class AC_current
{
private:
    double mag, phase;
    double real, imag;
    void setRect();
    void setPolar();
public:
    AC_current(double m=0, double p=0)
    {
        mag = m; phase = p; setRect();
    }
    AC_current operator-(const AC_current & c) const;
    AC_current operator+(const AC_current & c) const;
    AC_current operator*(double) const;
    int getCoord();
    friend std::istream &operator >> (std::istream &inst, AC_current &c);
    friend std::ostream &operator << (std::ostream &outst, AC_current &c);
};
int AC_current::getCoord()
{
    int selection;
    do
    {
        std::cout << "\n Select: \n\t1) AC current in polar coordinates ";
        std::cout << "\n\t2) AC current in rectangular coordinates ";
        std::cout << "\n\t Enter selection: ";
        std::cin >> selection;
    } while ((selection!=1)&&(selection!=2));
    return selection;
}
void AC_current::setRect()
{
    real= mag*cos(phase*Deg2Rad);
    imag=mag*sin(phase*Deg2Rad);
}
void AC_current::setPolar()
{
    mag=sqrt(real*real+imag*imag);
    if (!((real==0)&&(imag==0)))
    {
        phase=atan(imag/real)*Rad2Deg;
    }
    else
        phase=0;
}
AC_current AC_current::operator-(const AC_current &c) const
{
    AC_current temp;
    double treal, timag;
    treal = mag*cos(phase*Deg2Rad)-c.mag*cos(c.phase*Deg2Rad);
    timag = mag*sin(phase*Deg2Rad)-c.mag*sin(c.phase*Deg2Rad);
    temp.mag = sqrt(treal*treal+timag*timag);
    if (!((treal==0)&&(timag==0)))
    {
        temp.phase = atan(timag/treal)*Rad2Deg;
    }else temp.phase=0;
    return temp;
}
AC_current AC_current::operator+(const AC_current &c) const
{
    AC_current temp;
    double treal, timag;
    treal = mag*cos(phase*Deg2Rad)+c.mag*cos(c.phase*Deg2Rad);
    timag = mag*sin(phase*Deg2Rad)+c.mag*sin(c.phase*Deg2Rad);
    temp.mag = sqrt(treal*treal+timag*timag);
    if (!((treal==0)&&(timag==0)))
    {
        temp.phase = atan(timag/treal)*Rad2Deg;
    }else temp.phase=0;
    return temp;
}
AC_current AC_current::operator*(double r) const
{
    AC_current temp;
    temp.mag = mag*r;
    temp.phase = phase;
    temp.setRect();
    return temp;
}
std::istream &operator>>(std::istream &inst, AC_current &c)
{
    int choice=c.getCoord();
    if (choice==1)
    {
        std::cout << "\nEnter magnitude: ";
        inst >> c.mag;
        std::cout << "\nEnter phase shift: ";
        inst >> c.phase;
        c.setRect();
    }
    else if(choice == 2)
    {
        std::cout << "\nEnter real part: ";
        inst >> c.real;
        std::cout << "\nEnter imaginary part: ";
        inst >> c.imag;
        c.setPolar();
    }
    return inst;
}
std::ostream &operator<<(std::ostream &outst, const AC_current &c)
{
    outst << std::setiosflags(std::ios::fixed)<<std::setprecision(2);
    outst << "(" << std::setw(6) << c.mag;     
    outst << ", " << std::setw(6) << c.phase<<(char)248<< ") A"; 
    return outst;
}
int menu()
{
    int selection;
    do
    {
        std::cout << "\n\n\tSelect operation:" << std::endl;
        std::cout << "\n\n 1)Add currents" << std::endl;
        std::cout << "\n\n 2)Substract currents" << std::endl;
        std::cout << "\n\n 3)Multiply current by resistance" << std::endl;
        std::cout << "\n\n 4)Exit" << std::endl;
        std::cout << "\n\n Enter selection (1-4) => ";
        std::cin >> selection ;

    } while (selection<1||selection>4);
    return selection;
}
int main ()
{
    AC_current c1,c2;
    int operation;
    std::cin >> c1 >> c2;
    while (operation !=4) {
        operation = menu();
        switch (operation) {
            case 1:
                std::cout << "\n\tc1+c2 = " << (c1+c2);
                break;
            case 2:
                std::cout << "\n\tc1-c2 = " << (c1-c2);
                break;
            case 3:
                double r;
                std::cout <<"\nEnter resistance : ";
                std::cin >> r;
                std::cout << "\n\tc1*R = " << (c1*r);
                break;
            case 4:
                break;

        }
    }
    return 0;
}

0 个答案:

没有答案