代码之家  ›  专栏  ›  技术社区  ›  Crystal

C++帮助下的流抽取运算符重载

c++
  •  0
  • Crystal  · 技术社区  · 15 年前

    在C++中,我的流提取操作符重载了一个HW任务。我不确定为什么我会得到这些编译错误,因为我认为我做得对…这是我的代码:

    复合物

    #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
    

    络合物

    #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 << ')';
    }
    

    主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
    
    2 回复  |  直到 15 年前
        1
  •  0
  •   James McNellis    15 年前

    您在标题中注释掉的声明几乎是正确的。因为它是一个朋友,所以它不是一个成员,因此不能是const,所以它应该是:

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

    还请注意,您需要符合 ostream 作为 std::ostream ,因为你不应该使用 using namespace std; 在头文件中(你真的不需要在任何地方使用它;通常最好直接写出来 std:: 当您想使用标准库中的某些内容时)。

    同样,在源文件中,由于它不是成员函数,因此不在运算符定义前面加类名,它应该是:

    std::ostream &operator<<(std::ostream &output, const Complex &complexObj)
    
        2
  •  0
  •   Adam Holmberg    15 年前

    如果希望客户机代码访问运算符<<重载,请添加

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

    到你的类头文件。

    另一方面,如果只希望客户端代码调用print(),请删除

    Complex::
    

    从实现文件中的运算符定义确定作用域。