operator<< overload problem

This is a discussion on operator<< overload problem within the C++ Programming forums, part of the General Programming Boards category; I created complex class: Code: class CComplex { private: double m_dReal; double m_dImage; public: CComplex() :m_dReal(0),m_dImage(0){} CComplex(double re, double im=0) ...

  1. #1
    Registered User
    Join Date
    Jan 2009
    Posts
    20

    operator<< overload problem

    I created complex class:

    Code:
    class CComplex
    {
    private:
        double m_dReal;
        double m_dImage;
    public:
    	CComplex() :m_dReal(0),m_dImage(0){}
    	CComplex(double re, double im=0) :m_dReal(re),m_dImage(im){}
    	CComplex(const CComplex& x):m_dReal(x.getReal()),m_dImage(x.getImage()){}
    	double getReal () const {return m_dReal;}
    	double getImage ()const  {return m_dImage;}
    	CComplex operator+ (const CComplex& r)const;
    	CComplex& operator+= (const CComplex& r);
    	CComplex operator* (const CComplex& r)const;
    	CComplex& operator*= (const CComplex& r);
    	CComplex& operator= (const CComplex& r);
    	bool operator== (const CComplex& r) const;
    	bool operator!= (const CComplex& r) const;
    	friend ostream& operator << (ostream& ro, const CComplex& r);
    };
    when trying to compile i got the following error:
    ISO C++ forbids declaration of `ostream' with no type
    complex.H:34: error: `ostream' is neither function nor member function; cannot be declared friend
    complex.H:34: error: expected `;' before '&' token
    complex.C: In function `std:stream& operator<<(std:stream&, const CComplex&)':

    can't find what's wrong....

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,304
    You should fully qualify ostream as std::ostream. Additionally, you should #include <iosfwd>, and then #include <ostream> in the file where you define the overloaded operator<<.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jan 2009
    Posts
    20
    wow!!
    thanks..... you are the best!

  4. #4
    Registered User
    Join Date
    Mar 2009
    Posts
    3

    Similer problem

    i have the following class
    Code:
    #ifndef H_Student
    #define H_Student
    
    
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    class Student
    {
        friend ostream& operator<< (ostream&, const Student&);
    
        public:
            Student(string n="",float g = 0.0f,long j = 0):name(n),gpa(g),jagID(j){};
            string convertJag(long);
            friend class StudentNode;
            friend class NodeList;
            
             
        private:
            string name;
            float  gpa;
            long   jagID;
                
    };
    with ostream in .cpp

    Code:
    ostream& operator<< (ostream& output, const Student& s) 
    {
        output <<s.name<<", GPA = "<<s.gpa<<", JagID = "<<convertJag<<s.jagID;
        return output; 
    }

    the errors i get are:
    29 expected `,' or `...' before '&' token
    30 ISO C++ forbids declaration of `Student' with no type
    In function `std:stream& operator<<(std:stream&, int)':
    31 `s' undeclared (first use this function)
    (Each undeclared identifier is reported only once for each function it appears in.)
    [Build Error] [Student.o] Error 1


    what am i typing in wrong ive looked in 3-4 different books and they the same syntax for overloading<<

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,029
    It tells me that there is one or more types it doesn't recognize. You sure you includeed student.h in the .cpp file?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Registered User
    Join Date
    Mar 2009
    Posts
    3
    HAHA yea i forgot to include that. thanks. it always helps just to show someone else.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 07:54 PM
  2. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 06:24 PM
  3. no match for 'operator<<' in.... Compile Problem!
    By njd in forum C++ Programming
    Replies: 5
    Last Post: 01-09-2006, 12:40 PM
  4. Replies: 5
    Last Post: 11-07-2005, 11:34 PM
  5. half ADT (nested struct) problem...
    By CyC|OpS in forum C Programming
    Replies: 1
    Last Post: 10-26-2002, 09:37 AM

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