Thread: ostream operator overloading error

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    22

    ostream operator overloading error

    Header file for base class
    Code:
    #ifndef EMPLOYEE_H
    #define EMPLOYEE_H
    #include <string>
    using namespace std;
    
    class Employee
    {
        public:
            Employee(string Name="", int ID=0, char Class=' ', float Profit=0);
            virtual ~Employee();
            int getID();
            char getClass();
            float getProfit();
            void setName(string n);
            void setID(int id);
            void setClass(char c);
            void setProfit(float s);
            virtual void display(ostream & out) const;
        protected:
            string myName;
            int myID;
            char myClass;
            float myProfit;
        private:
    };
    inline ostream & operator << (ostream & out, const Employee & p)
    {
        p.display(out);
        return out;
    }
    Implementation file for base class
    Code:
    #include "Employee.h"
    #include <string>
    using namespace std;
    
    Employee::Employee(string Name, int ID, char Class, float Profit)
        :myName(Name), myID(ID), myClass(Class), myProfit(Profit)
    {
    }
    
    Employee::~Employee()
    {
        //dtor
    };
    int Employee::getID()
    {
        return myID;
    };
    char Employee::getClass()
    {
        return myClass;
    };
    float Employee::getProfit()
    {
        return myProfit;
    };
    void Employee::setName(string Name)
    {
        myName=Name;
    }
    void Employee::setID(int ID)
    {
        myID=ID;
    }
    void Employee::setClass(char Class)
    {
        myClass=Class;
    }
    void Employee::setProfit(float Profit)
    {
        myProfit=Profit;
    }
    void Employee::display(ostream & out) const
    {
        out << "Name:" << myName << endl
            << "ID#" << myID <<endl
            << "Class:" << myClass << endl
            << "Profit:" << myProfit << endl;
    
    }
    Implementation file for derived class
    Code:
    #include "Chef.h"
    #include <string>
    using namespace std;
    
    Chef::Chef(string Name, int ID, char Class, float Profit, string Expertise):Employee(Name,ID,Class,Profit),myExpertise(Expertise)
    {
        //ctor
    }
    
    Chef::~Chef()
    {
        //dtor
    }
    string Chef::getExpertise()
    {
        return myExpertise;
    }
    void Chef::setExpertise(string Expertise)
    {
        myExpertise=Expertise;
    }
    float Chef::Calc_Sal()
    {
        float x=getProfit();
        x*=.2;
        x+=10000;
        return x;
    }
    void Chef::display(ostream & out) const
    {
        Employee::display(out);
        out << "Salary:$" << Calc_Sal() << endl << "Expertise:" << myExpertise << endl;
    }
    When I try to build my code I am getting some errors related to the overloading of my '<<' operator. error: no match for 'operator<<' in 'out << "Salary:$"'|, and error: 'endl' was not declared in this scope| which i believe is related. If someone could help me out with this it would be greatly appreciated. THANK YOU!

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    You probably need to include an appropriate header somewhere along the line:

    Code:
    #include <ostream>
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Registered User
    Join Date
    Apr 2011
    Posts
    22
    can't believe i forgot to put that. thank you

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Nit: The "get" member functions should all be const.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. operator overloading error
    By manzoor in forum C++ Programming
    Replies: 3
    Last Post: 10-28-2008, 05:43 AM
  2. use <fstream> and ostream overloading together
    By mosu' in forum C++ Programming
    Replies: 3
    Last Post: 08-16-2006, 01:06 PM
  3. Operator Overloading (Bug, or error in code?)
    By QuietWhistler in forum C++ Programming
    Replies: 2
    Last Post: 01-25-2006, 08:38 AM
  4. issues using ostream while overloading []
    By Mr_Jack in forum C++ Programming
    Replies: 1
    Last Post: 01-12-2004, 12:00 PM
  5. operator overloading, long error
    By glorified ape in forum C++ Programming
    Replies: 1
    Last Post: 05-17-2003, 11:32 AM

Tags for this Thread