overloading <<

This is a discussion on overloading << within the C++ Programming forums, part of the General Programming Boards category; I'm trying to figure out how to overload &lt;&lt; so I can print a complex number just by cout &lt;&lt; ...

  1. #1
    Registered User
    Join Date
    Feb 2003
    Posts
    595

    overloading <<

    I'm trying to figure out how to overload << so I can print a complex number just by

    cout << c_num;

    I'm getting a compilation error saying
    k:\cfiles\cis285\complex\complex.h(24) : error C2270: '<<' : modifiers not allowed on nonmember functions

    How can I correct this?

    (The error message references the line that begins:
    "friend ostream& operator<<..."

    Code:
    #ifndef _complex_h
    #define _complex_h
    
    #include <iostream>
    using namespace std;
    
    
    class Complex{
    public:
    	Complex(double re = 0, double im = 0){_real = re, _imag = im;}
    	Complex operator+( const Complex& num2) const {
    		Complex result(_real + num2._real, _imag + num2._imag);
    		return result;}
    	Complex operator-( const Complex& num2) const {
    		Complex result(_real - num2._real, _imag - num2._imag);
    		return result;}
    	friend ostream& operator<<( ostream& os, const Complex& cnum) const {
    		return os << cnum._real	<< " + " << cnum._imag << "i";}
    
    	
    //	void print() {
    //		cout << _real << " + " << _imag << "i";}
    
    
    private:
    	double _real, _imag;
    
    };
    
    #endif

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,681
    There is a built in STL container for complex numbers, the << operator is already taken care of:
    Code:
    #include <complex>
    #include <iostream>
    using namespace std;
    
    int main()
    {
        complex<double> c1(3.2,6.1);
        cout << "Complex value c1: " << c1 << endl;
        return 0;
    }
    Should output something along the lines of:
    Complex value c1: (3.2,6.1)
    I used to be an adventurer like you... then I took an arrow to the knee.

  3. #3
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    Code:
    class Complex{
    public:
    	Complex(double re = 0, double im = 0){_real = re, _imag = im;}
    	Complex operator+( const Complex& num2) const {
    		Complex result(_real + num2._real, _imag + num2._imag);
    		return result;}
    	Complex operator-( const Complex& num2) const {
    		Complex result(_real - num2._real, _imag - num2._imag);
    		return result;}
    	//friend ostream& operator<<( ostream& os, const Complex& cnum) const {
    	//	return os << cnum._real	<< " + " << cnum._imag << "i";}
    	friend	ostream&	operator<<(ostream& os,const Complex& cnum){
    		return os << cnum._real	<< " + " << cnum._imag << "i";
    	}
    		
    //	void print() {
    //		cout << _real << " + " << _imag << "i";}
    
    
    private:
    	double _real, _imag;
    
    };

  4. #4
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,484
    Remove the "const" as The Dog pointed out.

    "Go Dawgs!"
    gg (UGA grad)

  5. #5
    Registered User
    Join Date
    Feb 2003
    Posts
    595
    Thanks, guys.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. overloading <<
    By msshapira in forum C++ Programming
    Replies: 2
    Last Post: 05-06-2009, 02:11 PM
  2. Overloading << and >>
    By MMu in forum C++ Programming
    Replies: 1
    Last Post: 04-21-2008, 06:49 AM
  3. Overloading fstream's << and >> operators
    By VirtualAce in forum C++ Programming
    Replies: 2
    Last Post: 04-09-2007, 03:17 AM
  4. Overloading << and >>
    By Enahs in forum C++ Programming
    Replies: 2
    Last Post: 09-15-2005, 04:33 PM
  5. Overloading the << operator.
    By antex in forum C++ Programming
    Replies: 5
    Last Post: 05-31-2005, 01:37 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21