Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Nov 10th, 2005, 11:08 AM   #1
-=PARADOX=-
Programmer
 
-=PARADOX=-'s Avatar
 
Join Date: Oct 2005
Location: Portugal
Posts: 53
Rep Power: 3 -=PARADOX=- is on a distinguished road
ostream

Hello to all...

I'm trying to redefine the output << and input >> operators, the prototype of the function in the class looks like this:

ostream& operator<<( ostream &stream, const CString );

When I compile ( i'm using c++ builder 6 ) it gives this errors on the above function:

Type name expected
Declaration missing

What i'm doing wrong???
-=PARADOX=- is offline   Reply With Quote
Old Nov 10th, 2005, 11:25 AM   #2
stevengs
Professional Programmer
 
stevengs's Avatar
 
Join Date: May 2005
Location: Bad Nauheim, Germany
Posts: 436
Rep Power: 4 stevengs is on a distinguished road
Quote:
Originally Posted by -=PARADOX=-
ostream& operator<<( ostream &stream, const CString );
...( ostream & stream, const CString what? )
__________________
-Steven
"Is this a piece of your brain?" - Basil Fawlty
stevengs is offline   Reply With Quote
Old Nov 10th, 2005, 11:29 AM   #3
Polyphemus_
Expert Programmer
 
Polyphemus_'s Avatar
 
Join Date: Aug 2005
Location: Rotterdam, the Netherlands
Posts: 942
Rep Power: 4 Polyphemus_ is on a distinguished road
Quote:
Originally Posted by stevengs
...( ostream & stream, const CString what? )
that doesn't matter, when defining you don't have to name the arguments passed to the function.
Polyphemus_ is offline   Reply With Quote
Old Nov 10th, 2005, 11:29 AM   #4
stevengs
Professional Programmer
 
stevengs's Avatar
 
Join Date: May 2005
Location: Bad Nauheim, Germany
Posts: 436
Rep Power: 4 stevengs is on a distinguished road
Quote:
Originally Posted by Polyphemus_
that doesn't matter, when defining you don't have to name the arguments passed to the function.
right... duh. But should it not be better a reference to a CString? Please provide the definition and an example call.
__________________
-Steven
"Is this a piece of your brain?" - Basil Fawlty

Last edited by stevengs; Nov 10th, 2005 at 11:42 AM.
stevengs is offline   Reply With Quote
Old Nov 10th, 2005, 12:22 PM   #5
-=PARADOX=-
Programmer
 
-=PARADOX=-'s Avatar
 
Join Date: Oct 2005
Location: Portugal
Posts: 53
Rep Power: 3 -=PARADOX=- is on a distinguished road
Sorry i forget the & ...

Here is the definition:

ostream& operator<<( ostream &stream, const CString &str1)
{
        stream << str;
        return stream;
}

Another question... should i implement the function inside the class or outside it using the friend keyword?
-=PARADOX=- is offline   Reply With Quote
Old Nov 10th, 2005, 12:59 PM   #6
Animatronic
Programmer
 
Join Date: Jun 2005
Posts: 99
Rep Power: 4 Animatronic is on a distinguished road
I always do inside the class def from simplicity e.g

class foo
{
	int a;
public:
	friend std::ostream& operator << ( std::ostream& os, foo const & f )
	{
		os << f.a;
		return os;
	}
};

btw isnt this:

ostream& operator<<( ostream &stream, const CString &str)
{
        stream << str; <<<<<<<<
        return stream;
}

going to cause an inifinte loop...
Animatronic is offline   Reply With Quote
Old Nov 10th, 2005, 1:13 PM   #7
-=PARADOX=-
Programmer
 
-=PARADOX=-'s Avatar
 
Join Date: Oct 2005
Location: Portugal
Posts: 53
Rep Power: 3 -=PARADOX=- is on a distinguished road
ups... i messed up things again... my code is:

ostream& operator<<( ostream &stream, const CString &str)
{
        stream << str.str; // str.str is a c style string
        return stream;
}

So I use the friend keyword, but where I declare that function?

By the way, c++ builder does't seem to like the ostream... for example:

#include <ostream>

int main()
{
        ostream stream;

        return 0;
}

It produces this error: "'Could not find match for 'ostream::basic_ostream()'"

Help...
-=PARADOX=- is offline   Reply With Quote
Old Nov 10th, 2005, 1:23 PM   #8
Animatronic
Programmer
 
Join Date: Jun 2005
Posts: 99
Rep Power: 4 Animatronic is on a distinguished road
Thats because ostream is in the std namespace, so you either need using namespace std; after your includes, or specify the namespace (like I did before) with std:: infront of the class/object/etc.

You need the friend because the operator << ( std::ostream, CString cons ) is not a member function and so the private member variables can not be accessed without friend before the definition. If you only acces const member functions within the operator then the operator dosent need to be a friend.
Animatronic is offline   Reply With Quote
Old Nov 10th, 2005, 1:27 PM   #9
-=PARADOX=-
Programmer
 
-=PARADOX=-'s Avatar
 
Join Date: Oct 2005
Location: Portugal
Posts: 53
Rep Power: 3 -=PARADOX=- is on a distinguished road
Even if i put 'using namespace std' it gives the same error.....
-=PARADOX=- is offline   Reply With Quote
Old Nov 10th, 2005, 1:38 PM   #10
Animatronic
Programmer
 
Join Date: Jun 2005
Posts: 99
Rep Power: 4 Animatronic is on a distinguished road
#include <iostream>

using namespace std;

class foo
{
	int a;
public:
	friend ostream& operator << ( ostream & os, foo const & f  )
	{
		os << f.a << std::endl;
		return os;
	}
};

int main()
{
	foo f;

	cout << f << endl;

	return 0;
}

That should work ^^ (it does for me).

What I said before about needed the friend because the operator wasnt a member function was wrong, its just the way I learnt it. There are some problems with the stream's not working with class operators only with global ones (because of the ordering of variables I think), maybe some one else with help clarify the stituation.
Animatronic is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 9:29 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC