473,499 Members | 1,658 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

problems overloading << operator

22 New Member
Hello!

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

my code is the following:

Expand|Select|Wrap|Line Numbers
  1. the student.h file
  2.  
  3. #include <string>
  4. #include <iostream>
  5.  
  6. using namespace std;
  7. class student
  8. {
  9.   private:
  10.     string name;
  11.     string department;
  12.   public:
  13.     student(string n = "", string dep = 0)
  14.     : name(n), department(dep) {}
  15.  
  16.     string get_name() const { return name; }
  17.     string get_department () const { return department; }
  18.     void set_name(const string& n) { name=n; }
  19.     void set_department (const string& d) {department=d;}
  20.     ostream& operator << (ostream& os, const student& s);
  21. };
  22.  
  23.  
Expand|Select|Wrap|Line Numbers
  1. the student.cpp file
  2.  
  3. ostream& student::operator << (ostream& os, const student& s)
  4. {
  5.   return os<<s.get_name()<<'\t'<<s.get_department()<<endl;
  6. }
  7.  
Expand|Select|Wrap|Line Numbers
  1. main.cpp file
  2.  
  3. #include "student.h"
  4. int main()
  5. {
  6.     student st("Bill Jones", "Zoology");
  7.     cout<<st;
  8. }
  9.  
the compiler tells me the following error message:

"student.h": E2080 'student::operator <<(ostream &,const student &)' must be declared with one parameter at line 18
May 15 '07 #1
4 1802
JosAH
11,448 Recognized Expert MVP
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
May 15 '07 #2
nomad5000
22 New Member
sorry about my ignorance, but exactly how do you declare a function a friend function

Thanks very much!!

Michael
May 15 '07 #3
AdrianH
1,251 Recognized Expert Top Contributor
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
May 15 '07 #4
weaknessforcats
9,208 Recognized Expert Moderator Expert
The way your class is designed, you don't need a friend function at all.

Your code is:
Expand|Select|Wrap|Line Numbers
  1. the student.cpp file
  2.  
  3. ostream& student:: operator << (ostream& os, const student& s)
  4. {
  5.   return os<<s.get_name()<<'\t'<<s.get_department()<<endl;
  6. }
  7.  
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:
Expand|Select|Wrap|Line Numbers
  1. the student.cpp file
  2.  
  3. ostream& operator << (ostream& os, const student& s)
  4. {
  5.   return os<<s.get_name()<<'\t'<<s.get_department()<<endl;
  6. }
  7.  
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.
May 16 '07 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

3
1624
by: Suresh Tri | last post by:
Hi all, I was trying to overload '<' operator for (varchar,varchar). But in the function which handles the comparision I want to use the previous '<' operator.. but it is going into a recursion....
6
1910
by: n2xssvv g02gfr12930 | last post by:
Does anyone know of an example of overloading operator ->* Cheers JB
1
2112
by: atomik.fungus | last post by:
Hi, as many others im making my own matrix class, but the compiler is giving me a lot of errors related to the friend functions which overload >> and <<.I've looked around and no one seems to get...
3
385
by: md | last post by:
Hi, the following code is working for static objects. ie the statement IntArray x(20); my problem is i want to use this overloading operator for dynamically created objects...
2
3532
by: nayannovellus | last post by:
As we know that both copy constructors and overloading = opeartor copies one object to another then whats the difference between copy constructor and overloading = operator. There must be some...
3
2043
by: Aff | last post by:
hi, i wold like to ask, why is it when overloading operator we send int value is there any way around?. e.g. class abc { protected: int *a; private: int operator(int );
6
1478
by: Peter v. N. | last post by:
Hi all, Maybe this has been asked a million times before. In that case I'm sorry for being to lazy to search the Internet or look it up in a decent C++ reference: I read in O'Reilly's C++...
11
4121
by: dascandy | last post by:
Hello, I was wondering, why is overloading operator. (period) forbidden? It would make a few odd applications possible (dynamic inheritance and transparent remote method invocation spring to my...
0
2006
by: citystud | last post by:
I am trying to convert the c++ code to C#, but find difficulty to convert the overloading operator. Here is the source code. I don't really know how to implement the operator = and operator () in...
0
7178
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7223
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6899
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7390
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5475
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4919
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3094
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
665
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
302
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.