![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Programmer
Join Date: Oct 2005
Location: Portugal
Posts: 53
Rep Power: 3
![]() |
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??? ![]() |
|
|
|
|
|
#2 | |
|
Professional Programmer
Join Date: May 2005
Location: Bad Nauheim, Germany
Posts: 436
Rep Power: 4
![]() |
Quote:
__________________
-Steven "Is this a piece of your brain?" - Basil Fawlty |
|
|
|
|
|
|
#3 | |
|
Expert Programmer
Join Date: Aug 2005
Location: Rotterdam, the Netherlands
Posts: 942
Rep Power: 3
![]() |
Quote:
|
|
|
|
|
|
|
#4 | |
|
Professional Programmer
Join Date: May 2005
Location: Bad Nauheim, Germany
Posts: 436
Rep Power: 4
![]() |
Quote:
__________________
-Steven "Is this a piece of your brain?" - Basil Fawlty Last edited by stevengs; Nov 10th, 2005 at 11:42 AM. |
|
|
|
|
|
|
#5 |
|
Programmer
Join Date: Oct 2005
Location: Portugal
Posts: 53
Rep Power: 3
![]() |
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? |
|
|
|
|
|
#6 |
|
Programmer
Join Date: Jun 2005
Posts: 99
Rep Power: 4
![]() |
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... |
|
|
|
|
|
#7 |
|
Programmer
Join Date: Oct 2005
Location: Portugal
Posts: 53
Rep Power: 3
![]() |
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... ![]() |
|
|
|
|
|
#8 |
|
Programmer
Join Date: Jun 2005
Posts: 99
Rep Power: 4
![]() |
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. |
|
|
|
|
|
#9 |
|
Programmer
Join Date: Oct 2005
Location: Portugal
Posts: 53
Rep Power: 3
![]() |
Even if i put 'using namespace std' it gives the same error.....
|
|
|
|
|
|
#10 |
|
Programmer
Join Date: Jun 2005
Posts: 99
Rep Power: 4
![]() |
#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. |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|