2010-05-03 122 views
0

我在C++中爲hw賦值重載我的流提取運算符時遇到了一些麻煩。我真的不知道爲什麼我收到這些編譯錯誤,因爲我以爲我這樣做是正確...這裏是我的代碼:運算符在C++幫助中重載流提取運算符

Complex.h

#ifndef COMPLEX_H 
#define COMPLEX_H 

class Complex 
{ 
    //friend ostream &operator<<(ostream &output, const Complex &complexObj) const; 
    // note at bottom regarding friend function 
public: 
    Complex(double = 0.0, double = 0.0); // constructor 
    Complex operator+(const Complex &) const; // addition 
    Complex operator-(const Complex &) const; // subtraction 
    void print() const; // output 
private: 
    double real; // real part 
    double imaginary; // imaginary part 
}; 

#endif 

Complex.cpp

#include <iostream> 
#include "Complex.h" 
using namespace std; 

// Constructor 
Complex::Complex(double realPart, double imaginaryPart) : real(realPart), imaginary(imaginaryPart) 
{ 
} 

// addition operator 
Complex Complex::operator+(const Complex &operand2) const 
{ 
    return Complex(real + operand2.real, imaginary + operand2.imaginary); 
} 

// subtraction operator 
Complex Complex::operator-(const Complex &operand2) const 
{ 
    return Complex(real - operand2.real, imaginary - operand2.imaginary); 
} 

// Overload << operator 
ostream &Complex::operator<<(ostream &output, const Complex &complexObj) const 
{ 
    cout << '(' << complexObj.real << ", " << complexObj.imaginary << ')'; 
    return output; // returning output allows chaining 
} 

// display a Complex object in the form: (a, b) 
void Complex::print() const 
{ 
    cout << '(' << real << ", " << imaginary << ')'; 
} 

的main.cpp

#include <iostream> 
#include "Complex.h" 
using namespace std; 
int main() 
{ 
    Complex x; 
    Complex y(4.3, 8.2); 
    Complex z(3.3, 1.1); 

    cout << "x: "; 
    x.print(); 
    cout << "\ny: "; 
    y.print(); 
    cout << "\nz: "; 
    z.print(); 

    x = y + z; 
    cout << "\n\nx = y + z: " << endl; 
    x.print(); 
    cout << " = "; 
    y.print(); 
    cout << " + "; 
    z.print(); 

    x = y - z; 
    cout << "\n\nx = y - z: " << endl; 
    x.print(); 
    cout << " = "; 
    y.print(); 
    cout << " - "; 
    z.print(); 
    cout << endl; 
} 

編譯錯誤回報:

complex.cpp(23) : error C2039: '<<' : is not a member of 'Complex' 
complex.h(5) : see declaration of 'Complex' 
complex.cpp(24) : error C2270: '<<' : modifiers not allowed on nonmember functions 
complex.cpp(25) : error C2248: 'Complex::real' : cannot access private member declared in class 'Complex' 
complex.h(13) : see declaration of 'Complex::real' 
complex.h(5) : see declaration of 'Complex' 
complex.cpp(25) : error C2248: 'Complex::imaginary' : cannot access private member declared in class 'Complex' 
complex.h(14) : see declaration of 'Complex::imaginary' 
complex.h(5) : see declaration of 'Complex' 

謝謝!

編輯:

I wasn't sure about declaring the friend function in the header file or not. When I do, I get these errors: 
c:\documents and settings\wongj\my documents\visual studio 2008\projects\c3_hw\c3_hw4\complex.h(6) : error C2143: syntax error : missing ';' before '&' 
1>c:\documents and settings\wongj\my documents\visual studio 2008\projects\c3_hw\c3_hw4\complex.h(6) : error C2433: 'ostream' : 'friend' not permitted on data declarations 
1>c:\documents and settings\wongj\my documents\visual studio 2008\projects\c3_hw\c3_hw4\complex.h(6) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int 
1>c:\documents and settings\wongj\my documents\visual studio 2008\projects\c3_hw\c3_hw4\complex.h(6) : error C2061: syntax error : identifier 'ostream' 
1>c:\documents and settings\wongj\my documents\visual studio 2008\projects\c3_hw\c3_hw4\complex.h(6) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int 
1>c:\documents and settings\wongj\my documents\visual studio 2008\projects\c3_hw\c3_hw4\complex.h(6) : error C2805: binary 'operator <<' has too few parameters 
1>c:\documents and settings\wongj\my documents\visual studio 2008\projects\c3_hw\c3_hw4\complex.cpp(23) : error C2872: 'ostream' : ambiguous symbol 
1>  could be 'c:\documents and settings\wongj\my documents\visual studio 2008\projects\c3_hw\c3_hw4\complex.h(6) : int ostream' 
1>  or  'c:\program files\microsoft visual studio 9.0\vc\include\iosfwd(708) : std::ostream' 
1>c:\documents and settings\wongj\my documents\visual studio 2008\projects\c3_hw\c3_hw4\complex.cpp(23) : error C2143: syntax error : missing ';' before '&' 
1>c:\documents and settings\wongj\my documents\visual studio 2008\projects\c3_hw\c3_hw4\complex.cpp(23) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int 
1>c:\documents and settings\wongj\my documents\visual studio 2008\projects\c3_hw\c3_hw4\complex.cpp(23) : error C2086: 'int ostream' : redefinition 
1>  c:\documents and settings\wongj\my documents\visual studio 2008\projects\c3_hw\c3_hw4\complex.h(6) : see declaration of 'ostream' 
1>c:\documents and settings\wongj\my documents\visual studio 2008\projects\c3_hw\c3_hw4\complex.cpp(23) : error C2872: 'ostream' : ambiguous symbol 
1>  could be 'c:\documents and settings\wongj\my documents\visual studio 2008\projects\c3_hw\c3_hw4\complex.h(6) : int ostream' 
1>  or  'c:\program files\microsoft visual studio 9.0\vc\include\iosfwd(708) : std::ostream' 
1>c:\documents and settings\wongj\my documents\visual studio 2008\projects\c3_hw\c3_hw4\complex.cpp(23) : error C2872: 'ostream' : ambiguous symbol 
1>  could be 'c:\documents and settings\wongj\my documents\visual studio 2008\projects\c3_hw\c3_hw4\complex.h(6) : int ostream' 
1>  or  'c:\program files\microsoft visual studio 9.0\vc\include\iosfwd(708) : std::ostream' 
1>c:\documents and settings\wongj\my documents\visual studio 2008\projects\c3_hw\c3_hw4\complex.cpp(23) : error C2065: 'output' : undeclared identifier 
1>c:\documents and settings\wongj\my documents\visual studio 2008\projects\c3_hw\c3_hw4\complex.cpp(23) : error C2059: syntax error : 'const' 
1>c:\documents and settings\wongj\my documents\visual studio 2008\projects\c3_hw\c3_hw4\complex.cpp(24) : error C2143: syntax error : missing ';' before '{' 
1>c:\documents and settings\wongj\my documents\visual studio 2008\projects\c3_hw\c3_hw4\complex.cpp(24) : error C2447: '{' : missing function header (old-style formal list?) 
1>Generating Code... 
1>Compiling... 
1>main.cpp 
1>c:\documents and settings\wongj\my documents\visual studio 2008\projects\c3_hw\c3_hw4\complex.h(6) : error C2143: syntax error : missing ';' before '&' 
1>c:\documents and settings\wongj\my documents\visual studio 2008\projects\c3_hw\c3_hw4\complex.h(6) : error C2433: 'ostream' : 'friend' not permitted on data declarations 
1>c:\documents and settings\wongj\my documents\visual studio 2008\projects\c3_hw\c3_hw4\complex.h(6) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int 
1>c:\documents and settings\wongj\my documents\visual studio 2008\projects\c3_hw\c3_hw4\complex.h(6) : error C2061: syntax error : identifier 'ostream' 
1>c:\documents and settings\wongj\my documents\visual studio 2008\projects\c3_hw\c3_hw4\complex.h(6) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int 
1>c:\documents and settings\wongj\my documents\visual studio 2008\projects\c3_hw\c3_hw4\complex.h(6) : error C2805: binary 'operator <<' has too few parameters 
+0

您忘了將'ostream&Complex :: operator <<(ostream&output,const Complex&complexObj)const'的原型添加到您的類定義中。 – 2010-05-03 22:51:14

回答

0

你在頭註釋掉的聲明幾乎是正確的。既然是朋友,它不是一個成員,因此不能是常量,所以它應該是:

friend std::ostream &operator<<(std::ostream &output, const Complex &complexObj); 

還要注意的是,你需要有資格ostreamstd::ostream,因爲你不應該在頭文件中使用using namespace std; (你真的不需要在任何地方使用它;當你想使用標準庫中的東西時,最好只寫出std::)。

同樣,在你的源文件,因爲它不是一個成員函數,你沒有前綴的類名的運營商定義的,它應該是:

std::ostream &operator<<(std::ostream &output, const Complex &complexObj) 
0

如果希望客戶端代碼有訪問操作< <超載,加上

ostream & operator<<(ostream &output, const Complex &complexObj) const 

到您的類的頭文件。

如果,另一方面,您只需要客戶端的代碼來調用印刷(),從實現文件操作定義中移除

Complex:: 

作用域。