2012-11-29 92 views
0

我使用這個類複雜的程序來解析方程無法理解的iostream錯誤

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>   ] 

請幫我試圖尋找身邊但答案似乎是特定於該程序,所以發佈了這個問題。

+1

無法重新創建,建立良好的VS 2010? – acraig5075

回答

5

您的complex班有一個std::stringstream的成員。當編譯器生成complex的拷貝構造函數時,它將嘗試調用std::stringstream的拷貝構造函數。但是這個失敗,因爲stringstream對象不能被複制。

我建議你刪除stringstream成員,只需在operator<<中將所需字段寫入輸出流即可。如果要使字符串表示成爲該類的成員,請改用std::string成員。

如果你真的想有一個stringstream成員(不推薦),你需要創建自己的拷貝構造函數,而不是編譯器生成一個。

+0

+1,這正是發生了什麼事。 – billz

+0

如果我將'complexStr = t.complexStr'添加到複製構造函數,我只會得到錯誤。沒有它,並根據OP的代碼,VS 2010構建的片段就好了? – acraig5075

+0

@ acraig5075:哦,我沒有注意到,有一個拷貝構造函數存在。但請注意,OP的拷貝構造函數需要一個非const引用,所以不能複製臨時對象。這個問題也可能會被代碼使用OP還沒有發佈的'complex'類引起的。 – interjay