problems overloading << operator

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • nomad5000
    New Member
    • Sep 2006
    • 22

    problems overloading << operator

    Hello!

    I'm trying to overload the << operator but it just won't work

    my code is the following:

    Code:
    the student.h file
    
    #include <string>
    #include <iostream>
    
    using namespace std;
    class student
    {
      private:
        string name;
        string department;
      public:
        student(string n = "", string dep = 0)
        : name(n), department(dep) {}
    
        string get_name() const { return name; }
        string get_department () const { return department; }
        void set_name(const string& n) { name=n; }
        void set_department (const string& d) {department=d;}
        ostream& operator << (ostream& os, const student& s);
    };
    Code:
    the student.cpp file
    
    ostream& student::operator << (ostream& os, const student& s)
    {
      return os<<s.get_name()<<'\t'<<s.get_department()<<endl;
    }
    Code:
    main.cpp file
    
    #include "student.h"
    int main()
    {
        student st("Bill Jones", "Zoology");
        cout<<st;
    }
    the compiler tells me the following error message:

    "student.h" : E2080 'student::opera tor <<(ostream &,const student &)' must be declared with one parameter at line 18
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    You have to make your overloaded operator<< function a friend function because
    the left operand of that expression insn't a object from your class (it's an ostream).

    kind regards,

    Jos

    Comment

    • nomad5000
      New Member
      • Sep 2006
      • 22

      #3
      sorry about my ignorance, but exactly how do you declare a function a friend function

      Thanks very much!!

      Michael

      Comment

      • AdrianH
        Recognized Expert Top Contributor
        • Feb 2007
        • 1251

        #4
        Originally posted by nomad5000
        sorry about my ignorance, but exactly how do you declare a function a friend function

        Thanks very much!!

        Michael
        Put friend in front of it in the prototype in the class declaration. Make a prototype outside of the class declaration (before the class declaration). Define the function in your source file. Note that it is not a member of the class, so you do not specify the class scope when defining the function.


        Adrian

        Comment

        • weaknessforcats
          Recognized Expert Expert
          • Mar 2007
          • 9214

          #5
          The way your class is designed, you don't need a friend function at all.

          Your code is:
          [Code=cpp]
          the student.cpp file

          ostream& student:: operator << (ostream& os, const student& s)
          {
          return os<<s.get_name( )<<'\t'<<s.get_ department()<<e ndl;
          }
          [/CODE]

          Nothing in this code requires access to the class private data. It uses accessor functions instead. You need to:

          1) remove the operator<< function prototype from your class
          2) change this code to not be a member function:
          [Code=cpp]
          the student.cpp file

          ostream& operator << (ostream& os, const student& s)
          {
          return os<<s.get_name( )<<'\t'<<s.get_ department()<<e ndl;
          }
          [/CODE]
          and things should work fine.

          The only time you need a friend function is when the first argument cannot be a "this" pointer AND the function needs access to the class private data elements. This is not your situation.
          Last edited by JosAH; May 16 '07, 05:34 AM. Reason: added spaces to avoid a bug in [code] processing

          Comment

          Working...