我在程序中使用此类复杂的类来解析方程式

class complex
{
    double real;
    double imag;
    stringstream complexStr;

public:
    complex(double re = 0, double im = 0)
    {
        real = re;
        imag = im;
        complexStr<<real<<"+j"<<imag;
    }

    complex(complex &t)
    {
        real = t.real;
        imag = t.imag;
    }

    void StrtoComplex(char *temp)
    {
        int i;

        for(i = 0; i < strlen(temp); i++)
            if(temp[i] == 'j' || temp[i] == 'i')
                break;

        real = atof(temp);//takes till the last valid char so after + or whitespace it ignores
        imag = atof(temp + i + 1);

        complexStr<<real<<"+j"<<imag;
    }

    friend complex operator+(complex &a, complex &b);
    friend complex operator-(complex &a, complex &b);
    friend complex operator-(complex &a);
    friend complex operator*(complex &a, complex &b);
    friend complex operator/(complex &a, complex &b);
    friend ostream &operator<<(ostream &s, complex &t);
    friend istream &operator>>(istream &s, complex &t);
};

//overloading + to add complex numbers
complex operator +(complex &a, complex &b)
{
    complex t;
    t.real = a.real + b.real;
    t.imag = a.imag + b.imag;
    return(t);
}
//overaloading - to subtract 2 complex no's
complex operator -(complex &a, complex &b)
{
    complex t;
    t.real = a.real - b.real;
    t.imag = a.imag - b.imag;
    return(t);
}

//overloading unary -
complex operator -(complex &a)
{
    complex t(-a.real, -a.imag);
    return(t);
}

//overloading * to multiply 2 complex no's
complex operator *(complex &a, complex &b)
{
    complex t;
    t.real = (a.real*b.real) - (a.imag*b.imag);
    t.imag = (a.real*b.imag) + (a.imag*b.real);
    return(t);
}
//overloading / to divide 2 complex no's
complex operator /(complex &a, complex &b)
{
    complex t;
    t.real = ((a.real*b.real) + (a.imag*b.imag))/(b.real*b.real + b.imag*b.imag);
    t.imag = ((a.real*b.imag) - (a.imag*b.real))/(b.real*b.real + b.imag*b.imag);
    return(t);
}

ostream &operator<<(ostream &s, complex &t)
{
    s<<t.complexStr.str();
    return s;
}

istream &operator>>(istream &s, complex &t)
{
    char *temp;

    s>>temp;
    t.StrtoComplex(temp);
    return s;
}


我收到此错误:

 error C2248: 'std::basic_ios<_Elem,_Traits>::basic_ios' : cannot access private member declared in class 'std::basic_ios<_Elem,_Traits>'
1>          with
1>          [
1>              _Elem=char,
1>              _Traits=std::char_traits<char>
1>          ]
1>          c:\program files (x86)\microsoft visual studio 10.0\vc\include\ios(176) : see declaration of 'std::basic_ios<_Elem,_Traits>::basic_ios'
1>          with
1>          [
1>              _Elem=char,
1>              _Traits=std::char_traits<char>
1>          ]
1>          This diagnostic occurred in the compiler generated function 'std::basic_stringstream<_Elem,_Traits,_Alloc>::basic_stringstream(const std::basic_stringstream<_Elem,_Traits,_Alloc> &)'
1>          with
1>          [
1>              _Elem=char,
1>              _Traits=std::char_traits<char>,
1>              _Alloc=std::allocator<char>
1>          ]


请帮助我尝试搜索,但答案似乎特定于该程序,因此发布了此问题。

最佳答案

您的complex类有一个std::stringstream成员。当编译器为complex生成副本构造函数时,它将尝试为std::stringstream调用副本构造函数。但这失败了,因为无法复制stringstream对象。

建议您删除stringstream成员,而只需将所需的字段写入operator<<中的输出流。如果要使字符串表示形式成为类的成员,请改用std::string成员。

如果确实要有一个stringstream成员(不推荐),则需要创建自己的副本构造函数,而不是由编译器生成的副本构造函数。

关于c++ - 无法了解iostream错误,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/13627320/

10-12 14:55