Thread: need help with overloading << , please

  1. #1
    Registered User
    Join Date
    Feb 2005
    Posts
    3

    need help with overloading << , please

    Hello , when I try to compile the following code the compiler gives me the following error :
    error C2804: binary 'operator <<' has too many parameters

    the code makes no sense , I just wanted to learn how to use the overloading with any kind of program. Here is the code :

    Code:
    #include <iomanip>
    #include <iostream>
    #include <ostream>
    #include <fstream>
    #include <string>
    
    using namespace std;
    
    class name{
    
    public:
    	void display(void);
    	void assing(string t1,string t2);
     ostream & operator<< (ostream &k , name &obj){
    
    	k<<obj.d1<<obj.d2<<endl;
    	}
    
    private:
    
    	string d1,d2,d3;
    
    };
    
    
    
    void name::assing(string t1,string t2){
    	d1=t1;
    	d2=t2;
    }
    void name::display(void){
    	cout<<d1<<endl<<endl;
    	cout<<d2<<endl<<endl;
    
    }
    
    int main(){
    	string s1="aaa", s2="bbbb";
    name c;
     c.assing(s1,s2);
    cout<<c;
    
    	return 0;
    
    
    }
    Thank you.

  2. #2
    Registered User
    Join Date
    Feb 2005
    Posts
    3

    need help with overloading << , please

    sorry I forgot to include the return k; , but even returning it gives me the same error.

  3. #3
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    operator << needs to be made a friend function and defined outside of the class. The error you are getting is because you are trying to define the operator<< function that lets you do something like:
    Code:
     Obj obj;
    obj <<"Hello World";
    Simply change it to:
    Code:
    friend  ostream & operator<< (ostream &k , name &obj);
    and outside the class put the function.

  4. #4
    Registered User
    Join Date
    Feb 2005
    Posts
    3

    Re:need help with overloading << , please

    Thantos Thank you , I solved my problem with your help.

  5. #5
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Hi,

    When you do this:

    cout<<c;

    You are using the ostream class's insertion operator(cout is an instance of the ostream class). So, trying to redefine the ostream class's insertion operator in your class doesn't work. In your class, you are supposed to define things that are executed when called on your class members--not other class members.

    If instead, you define the insertion operator in a global function outside your class, then I think it provides a unique function signature(ala function overloading), which will be called when you use cout's insertion operator with members of your class. I don't quite understand the details of why the global function will get called instead of the compiler throwing an error when it searches the ostream class and can't find an <<operator function that has your class member as a parameter. Maybe Thantos can comment on that.
    Last edited by 7stud; 02-16-2005 at 07:44 PM.

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