473,378 Members | 1,387 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

issues with overloading the output operator

momotaro
357 100+
am trying to overload my output operator but it gives me these errors:

error C2143: syntax error : missing ';' before '&'

error C2433: 'ostream' : 'friend' not permitted on data declarations

error C4430: missing type specifier - int assumed. Note: C++ does not support default-int

error C2061: syntax error : identifier 'ostream'

error C4430: missing type specifier - int assumed. Note: C++ does not support default-int

error C2805: binary 'operator <<' has too few parameters

here my prototype function in the header file:
Expand|Select|Wrap|Line Numbers
  1. friend ostream& operator<<(ostream &out, MyPoint &p);
and here is my implementation:
Expand|Select|Wrap|Line Numbers
  1. ostream& operator<<(ostream &out, MyPoint &m)
  2. {
  3.     return out<< "X = " <<m.getAbs() << "Y = " <<m.getOrd() <<endl;
  4. }
Apr 4 '11 #1
18 4327
Oralloy
985 Expert 512MB
Try dropping the keyword friend from your declaration.

The reason is that your function is just that, a function; it is not a method of any class.
Apr 4 '11 #2
momotaro
357 100+
thank's Oralloy but this is not how we overload the output stream operator!
if you have any other suggestion please feel free.
PS: the keyword friend here is mandatory !
Apr 4 '11 #3
weaknessforcats
9,208 Expert Mod 8TB
From your posting, Oralloy is correct and the friend keyword is in error. A friend can only occur inside a class declaration and what you have posted is not inside a class declaration.

Then, the compiler has a problem with the &. That means that by the time the compiler reached an & it was in trouble. Most likely, either ostream or MyPoint has no been declared before being used. Check that yu have included the appropriate headers.
Apr 4 '11 #4
Oralloy
985 Expert 512MB
momotaro,

Look at the bottom of this page for some template examples of what you're trying to implement.

Good Luck!
Apr 4 '11 #5
momotaro
357 100+
when I posted I assumed that the line of code being in a class declaration is obvious my bad here is my hole code:
Expand|Select|Wrap|Line Numbers
  1. #include "Vector.h"
  2. class MyPoint
  3. {
  4.  
  5. private:
  6.     double abs;
  7.     double ord;
  8.  
  9. public:
  10.     MyPoint(double x=0, double y=0);
  11.     double getAbs(void);
  12.     void setAbs(double);
  13.     double getOrd(void);
  14.     void setOrd(double);
  15.     bool coincide(MyPoint);
  16.     static bool coincide_static(MyPoint, MyPoint);
  17.     MyPoint operator+(MyPoint);
  18.     MyPoint operator++();
  19.     MyPoint operator++(int);
  20.     void affiche();
  21.     friend bool coincide_friend(MyPoint, MyPoint);
  22.     MyPoint translate(Vector v);
  23.     MyPoint multiple(Vector v);
  24.     friend ostream& operator<<(ostream &out, MyPoint &p);
  25. };
Apr 4 '11 #6
Oralloy
985 Expert 512MB
momotaro,

If I understand your code, ostream isn't declared in your module, unless it's coming indirectly from Vector.h

You can cheat and use a foreward reference of the sort
Expand|Select|Wrap|Line Numbers
  1. class std::ostream;
Or, you can simply include the appropriate C++ header at the top of your file.

Luck!
Apr 4 '11 #7
momotaro
357 100+
It was the first thing I tried out but no chance still giving me the same error could it be a bug in the compiler or something?? I've read that somewhere...
thank's in advance
Apr 4 '11 #8
Oralloy
985 Expert 512MB
momotaro,

Why does the overloaded operator need to be declared a friend of your class, anyway? It only uses the public interface, from what I see.

On the other hand, you might be experiencing a problem with usage. This is microsoft, and I don't remember if they normally import the std namespace into the root namespace or not.

If you #include <iostream>, instead of #include <iostream.h>, it might help.

Also, you might change the friend declaration as such:
Expand|Select|Wrap|Line Numbers
  1.     friend std::ostream& operator<<(std::ostream &out, MyPoint &p);
Cheers!
Apr 4 '11 #9
weaknessforcats
9,208 Expert Mod 8TB
You can't use ostream& unless you have declared an ostream class. That means this won't work:

Expand|Select|Wrap|Line Numbers
  1. class ostream;
A forward reference is OK when you need a pointer to an object but for a reference to an object the compiler needs the full class declaration to insure you use the reference correctly.

#include <iostream>
Apr 4 '11 #10
Oralloy
985 Expert 512MB
weaknessforcats,

I did not realize that. I thought a forward reference was acceptable, assuming a correct type match and none of the class details were accessed in the referencing code. (in this case, the friend declaration)

Good to learn something.

Thank You.
Apr 4 '11 #11
momotaro
357 100+
If I take out the keyword friend gives me another error:
too many parameters
PS: iostream is included but still giving me ostream undefined

driving me crazy :(

thx in advance
Apr 4 '11 #12
Oralloy
985 Expert 512MB
momotaro,

did you try using std::ostream, instead?
Apr 4 '11 #13
momotaro
357 100+
yes! and now without the keyword friend its telling me too many param in the header and too few in the definition file
:D how is it even possible
thx in advance
Apr 4 '11 #14
Oralloy
985 Expert 512MB
momotaro,

The iostreams are declared and defined in the std:: namespace.

What is getting you is how microsloth deals with namespaces. I have been bitten by this problem before, and if you include <iostream.h> you get different namespace usage than if you use <iostream>.

If you don't know what a namespace is, think of it as a class where all methods are public-static, and no attributes. Obviously, one can't instantiate a namespace. All it is is a means of isolating related functions and data from other packages. You will sometimes find them in larger projects that break down into logical sub-components. I'm afraid that most developers seem to consider them a pain, rather than the utility that they are.

All that said, I still don't understand why you need that friend declaration in your class at all.

Good Luck!
Apr 4 '11 #15
momotaro
357 100+
lets say I took it out!
now my overloading method is as follows: (assuming imports are correct)
Expand|Select|Wrap|Line Numbers
  1. std::ostream& operator<<(std::ostream& out);
which is logic since its a member function I "normaly could use this to access the calling object"
the definition is as follows:
Expand|Select|Wrap|Line Numbers
  1. std::ostream& operator<<(std::ostream& out, point p)
  2. {
  3.     out<<p.abs << " " << p.ord <<endl;
  4. }
now why in hell it didn't mentioned the deference between the number of arg between the declaration and teh definition and why it tells me I can't access my variables trough "this"

pulling my hears off :(
Apr 4 '11 #16
Oralloy
985 Expert 512MB
momotaro,

Do you know what a friend declaration does?

I'm saying that you do not need the declaration of operator<< in your class at all.
Apr 4 '11 #17
momotaro
357 100+
in this case where should I declare it?
I looked everywhere and here is a link fro example:
http://www.java2s.com/Tutorial/Cpp/0...adinserter.htm
Apr 4 '11 #18
Oralloy
985 Expert 512MB
momotaro,

Your example has the << operator declared as friend inside the class phonebook, because it uses the private attributes of the class directly. In your case, your operator is using the public interface of MyPoint, so declaring it as friend is unnecessary.

How you declare the operator depends on how your project is structured....

You could have a separate header and c++ file for the operator. In which case, the header file simply declares as
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <MyPoint.h>
  3.  
  4. std::ostream& operator<<(std::ostream& out, MyPoint &p);
You could simply in-line the entire operator in the header and dispense with the implementation file:
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <MyPoint.h>
  3.  
  4. inline std::ostream& operator<<(std::ostream& out, MyPoint& p)
  5. {
  6.     out<<p.abs << " " << p.ord <<endl; 
  7. }
Or, you can simply define the operator at the top of the single file that you use it in, and dispense with any abstract declaration.

It all really depends on how you are structuring your project, and what your goals are.

Cheers!
Apr 4 '11 #19

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

Similar topics

3
by: David.H | last post by:
Good evening everyone, Im using this code to get an idea on overloading the + operator: class Graph { public: Graph(void); Graph(int valX, int valY); Graph operator+(const Graph&); int...
3
by: W. Cerven | last post by:
I am using the C++ STL container "list" to create a template nested list. Attached are my basic code and compiler errors. Oddly enough, I when I create a non-template version of this nested list,...
4
by: hall | last post by:
Hi all. I have run into a problem of overloading a templatized operator>> by a specialized version of it. In short (complete code below), I have written a stream class, STR, which defines a...
2
by: Peter | last post by:
Hello, Thanks for reviewing my question. I am trying to override the = operator for my UserControl class; However, I am getting a syntax error. public class MyClass { private int x = 0; ...
3
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....
3
by: Abubakar | last post by:
Hi, lets say I have a class called "hashstring". I want to be able to write the following code: hashstring hs ( "hello world" ); char * somecharptr; somecharptr = hs; // here the somecharptr...
5
by: raan | last post by:
What I am trying to achieve here is depicted in the small program below. // Wrapit.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include <map> #include...
16
by: EM.Bateman | last post by:
Working on Visual Studio .Net I've implemented a class: #ifndef CONTRIBUTOR_H #define CONTRIBUTOR_H enum Gender {male=1, female, unk}; #include <iostream> #include <iomanip> #include...
4
by: UofFprogrammer | last post by:
Hello, I was taking a look at overloading the = operator in a C Plus Plus book. Part of the code of the overloaded =operator included checking for the case when the same object was on both sides of...
9
by: Faisal | last post by:
Hi, Why C++ doesn't allow overloading of size of operator. I think it would be much handy to check the type userdefined types. For eg. In my project, I've some structures which contains...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.