Thread: Newbie overloading << operator (have searched, been good, but...)

  1. #1
    Registered User
    Join Date
    Oct 2004
    Posts
    24

    Post Newbie overloading << operator (have searched, been good, but...)

    Hi,

    Firstly, many thanks in advance for any help or advice, it is very much appreciated.

    I am trying to overload the << operator to output the data members 'loan' and 'name' for my student class.

    Here is my setup (i have kept it very, very simple, in hope of a clear response). It might seem like a lot, but it is just 2 set methods and two get methods.

    Code:
    using namespace std;
    
    Student::Student()
    {
            loan = 0;
    	name = "";
    }
    
    Student::Student(int extra)
    {
    	loan = extra;
    	name = "";
    }
    
    void Student::setLoan(int amount)
    {
    	loan += amount;
    }
    
    void Student::setName(string word)
    {
    	name = word;
    }
    
    int Student::getLoan()
    {
    	return loan;
    }
    
    string Student::getName()
    {
    	return name;
    }
    
    Student::operator<< (ostream &out, Student)
    {
    	Student studentn;
    	out << studentn.getName();
    	out << studentn.getLoan();
    
    	return studentn;
    }
    Here is my header file:

    Code:
    class Student
    {
    	private:
    		int loan;
    		string name;
    
    	public:
    		Student();
    		Student(int);
    		void setLoan(int);
    		int getLoan();
    		void setName(string);
    		string getName();
    		Student operator<< (ostream &out, Student);
    };
    This is my main function:

    Code:
    void main()
    {
    
    	int tmpLoan = 300;
    	string tmpName = "Bruno";
    
    	Student bruno (100);
    
    	bruno.setLoan(tmpLoan);
    	bruno.setName(tmpName);
    
    	//cout << bruno.getLoan() << endl;
    	//cout << bruno.getName() << endl;
    
    	cout << bruno;
    }
    I am getting the following errors:

    c:\visual studio projects\studentoverloader\student.h(18) : error C2804: binary 'operator <<' has too many parameters
    c:\visual studio projects\studentoverloader\student.cpp(41) : error C2556: 'int __thiscall Student:perator <<(class std::basic_ostream<char,struct std::char_traits<char> > &,class Student)' : overloaded function differs only by return type from 'c
    lass Student __thiscall Student:perator <<(class std::basic_ostream<char,struct std::char_traits<char> > &,class Student)'
    c:\visual studio projects\studentoverloader\student.h(18) : see declaration of '<<'
    c:\visual studio projects\studentoverloader\student.cpp(41) : error C2371: '<<' : redefinition; different basic types
    c:\visual studio projects\studentoverloader\student.h(18) : see declaration of '<<'
    main.cpp
    I really really would appreciate any help at all, it is not urgent, or for anything in particular, i am just stuck.

    Best regards, global

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Try a:
    friend std::ostream& operator<<(std::ostream& out, const Student& student)

    I think you'll need to implement it within your class declaration, I havent really played around with overloading i/ostream as friend functions so I'm not sure.
    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
    Oct 2004
    Posts
    24
    Thank you for your quick reply, it is very much appreciated.

    Your code / better implementation of overloading reduced a lot of errors (nearly all of them in fact). I just have a small problem.

    If i use:

    Code:
    class Student
    {
    	private:
    		int loan;
    		string name;
    
    	public:
    		friend std::ostream& operator<<(std::ostream& out, const Student& student);
    };
    With

    Code:
    Student ostream& operator<<(std::ostream& out, const Student& student)
    {
    	Student studentn;
    	out << studentn.getName();
    	out << studentn.getLoan();
    
    	return student;
    }
    In my source/implementation file i get quite a few errors (about 5 nasty ones, it looks quite wrong).

    But if i change it too:

    Code:
    ostream& operator<<(std::ostream& out, const Student& student)
    {
    	Student studentn;
    	out << studentn.getName();
    	out << studentn.getLoan();
    
    	return out;
    }
    I dont get any compile errors at all. However, it does not work because all i get is '0' in the output.

    However, thanks to your help, i feel i am almost there. Apologies if i am being stupid.

    Best regards, global

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    instead of initialising a studentn object, what if you use the student object passed to the function?
    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

  5. #5
    Registered User
    Join Date
    Oct 2004
    Posts
    24
    Thank you.

    I did try that and i got the same error repeated 3 times:

    'getName' : cannot convert 'this' pointer from 'const class Student' to 'class Student &'

    'getLoan' : cannot convert 'this' pointer from 'const class Student' to 'class Student &'

    error C2440: 'return' : cannot convert from 'const class Student' to 'class std::basic_ostream<char,struct std::char_traits<char> > &'

    Thank you, once again for your help and advice. It is very much appreciated.

    Best regards, global

  6. #6
    Registered User
    Join Date
    Oct 2004
    Posts
    24
    yay - i got it to work (when i say 'i', what i mean is 'you' ) - thanks, i changed it to:

    return out;

    and got rid of the constants.

    I am a very happy person.

    Thank you for your quick responses and help.

    Best regards, global

  7. #7
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Keep the constants and make the getter functions const:
    int getLoan() const;
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  8. #8
    Registered User
    Join Date
    Apr 2004
    Posts
    29

    question regarding operator.

    I'm beginning to understand these overloaded operators now I think.
    Code:
    Student::operator<< (ostream &out, Student)
    {
    	Student studentn;
    	out << studentn.getName();
    	out << studentn.getLoan();
    
    	return studentn;
    }
    or I guess now it's
    Code:
    Student::operator<< (ostream &out, Student)
    {
    	Student studentn;
    	out << studentn.getName();
    	out << studentn.getLoan();
    
    	return out;
    }
    is better than just having a function like
    Code:
    Student::Output(Student)
    {
    Student studentn;
    cout << studentn.getName();
    .
    .
    .
    }
    . How so is this?

  9. #9
    Registered User
    Join Date
    Oct 2004
    Posts
    24
    It's better than having a normal function because it is overloaded. You have all the functionality of the normal cout << operator, only with as much other functionality as you want. If you tried to replicate all of that inside a function it could get pretty messy. The compiler knows which << operator you are trying to use automatically.

    Regards, global

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 05-07-2007, 06:49 PM
  2. Overloading: What is it good for!?
    By Sentral in forum C++ Programming
    Replies: 12
    Last Post: 08-26-2006, 02:40 AM
  3. Good newbie projects...
    By CompiledMonkey in forum C++ Programming
    Replies: 5
    Last Post: 10-20-2004, 06:40 AM
  4. Operator Overloading newbie question
    By Panopticon in forum C++ Programming
    Replies: 3
    Last Post: 01-03-2003, 07:14 PM
  5. Newbie asking about overloading functions
    By GLR in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2001, 10:54 AM