Thread: operator<< overload problem

  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
    28,413
    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<<.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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
    Location
    Inside my computer
    Posts
    24,654
    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, 06: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, 08:37 AM