【问题标题】:Trouble with istream overload in descendant class c++后代类c ++中的istream重载问题
【发布时间】:2015-04-27 15:41:02
【问题描述】:

我认为我的程序的插入运算符重载有问题。这是针对初学者 c++ 课程的作业,我应该在其中使用后代函数来执行具有复数和向量的任务。 当我为任一类输入一个数字时,它要么没有被正确读入,要么没有正确分配给数组。我已经尝试解决这个问题一个多小时了,但我尝试的任何方法似乎都不起作用。

课程:

class pairs
{
protected:
    double a;
    double b;
public: 
    pairs(): a(0), b(0){}
    pairs(const pairs&p): a(p.a), b(p.b){}
    pairs(double x, double y): a(x), b(y){}
    pairs operator +(pairs& second){
        return pairs(a+second.a, b+second.b);
    }
    pairs operator -(pairs& second){
        return pairs(a-second.a, b-second.b);
    }
    bool operator ==(pairs& second){
        bool tf=false;
        if (a==second.a && b==second.b)
            tf=true;
        return tf;
    }
};
class comp:public pairs
{
public:
    comp():pairs(){}
    comp(const pairs&p):pairs(p){}
    comp(double x, double y):pairs(x, y){}
    comp operator *(comp& second){
        comp mew;
        mew.a=(a*second.a)-(b*second.b);
        mew.b=(a*second.b)+(b*second.a);
        return mew;
    }
    comp operator /(comp& second);
    friend ostream& operator << (ostream& fout, comp& num){
        if(num.b<0)
            cout<<num.a<<num.b<<"i";
        else
            cout<<num.a<<"+"<<num.b<<"i";
        return fout;
    }
    friend istream& operator >> (istream& fin, comp num){
        char sym, i;
        cout<<"Enter a complex number in a+bi or a-bi form: ";
        cin>>num.a>>sym>>num.b>>i;
        if(sym=='-')
            num.b*=-1;
        return fin;
    }
};
class vect:public pairs
{
public:
    vect():pairs(){}
    vect(const pairs&p):pairs(p){}
    vect(double x, double y):pairs(x, y){}
    vect operator*(double num){
        return vect(a*num, b*num);
    }
    int operator*(vect num){
        int j;
        j=(a*num.a)+(b*num.b);
        return j;
    }
    friend ostream& operator << (ostream& fout, vect& num){
        cout<<"<"<<num.a<<","<<num.b<<">";
        return fout;
    }
    friend istream& operator >> (istream& fin, vect num){
        char beak, com;
        cout<<"Enter vector in <a,b> form: ";
        cin>>beak>>num.a>>com>>num.b>>beak;
        return fin;
    }
};

对插入运算符的调用如下所示:

        comp temp;
        int store;
        cin>>temp;
        cout<<"Where do you want to store this (enter 1-6): ";
        cin>>store;
        while(store<1 || store>6)
        {
            cout<<"Invalid location. re-enter: ";
            cin>>store;
        }
        six[store-1]=temp;
        break;

如果将 'comp temp' 更改为 'vect temp',对于 vect 也是一样的 一个 comp 或 vect 大小为 6 的数组被传递到函数中,这就是为什么 Six[] 未显示为被声明的原因。

在将它分配给数组之前,我尝试运行程序并打印 temp,这两个值仍然为零,我不知道为什么会这样。

非常感谢任何建议。 :]

【问题讨论】:

  • 我能给出的最佳建议是:在调试器中运行该代码并逐行逐行执行,以缩小问题的根源。
  • 参数应该是comp&amp; num,并且&gt;&gt;不期望执行任何用户交互——只从给定的流中读取。
  • 感谢@molbdnilo 问题出在参数上!现在完美运行。 :]

标签: c++ class overloading operator-keyword descendant


【解决方案1】:

您应该在operator&gt;&gt;() 实现中使用std::istream&amp; 来读取:

friend istream& operator >> (istream& fin, vect num){
    char beak, com;
    // cout<<"Enter vector in <a,b> form: ";
    fin >> beak >> num.a >> com >>num.b >> beak;
 // ^^^
    return fin;
}

对于其他 operator&gt;&gt;() 重载也是如此。

您还想在使用重载输入函数之外输出提示(这就是我在上面评论它的原因)。

【讨论】:

    猜你喜欢
    • 2011-07-07
    • 2011-09-23
    • 1970-01-01
    • 2023-03-15
    • 1970-01-01
    • 1970-01-01
    • 2012-08-16
    • 2020-02-10
    • 2012-12-22
    相关资源
    最近更新 更多