Custom Cout<< method

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Jul 2008
Posts: 4
Reputation: williamnz is an unknown quantity at this point 
Solved Threads: 0
williamnz williamnz is offline Offline
Newbie Poster

Custom Cout<< method

 
0
  #1
Jul 31st, 2008
Hi there,

I have a custom class , lets call it MyClass. In the main part of my prog I want to run a cout which outputs say Myinstance. I would have though I would add it nt he stdiolib. But I want to add it in my class... Something along the lines of

class MyClass {

MyClass::Cout {
print. MyInstance.x + " " + MyInstance.y;
}
}

Something like that, I'm not doing it properly as, it is giving me the error: "no match for operator<< in std::operator<<
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 401
Reputation: CoolGamer48 is on a distinguished road 
Solved Threads: 40
CoolGamer48's Avatar
CoolGamer48 CoolGamer48 is offline Offline
Posting Pro in Training

Re: Custom Cout<< method

 
0
  #2
Aug 1st, 2008
All you're doing is defined a method of MyClass called Cout. To, use it, you would have to call it:
  1. MyClass obj;
  2. obj.Cout();

the cout you use to do things like cout << x; is not a function, it's an object. Look in to operator overloading, if you haven't already.

To allow you to use the insertion (shift-left) operator (<<) with left operand of ostream (the class that cout is an object of) and right operand of MyClass, do something like this (I haven't actually run this, so it may not work):
  1. ostream& operator<<(ostream& cout,MyClass obj)
  2. {
  3. cout << "Output members here.";
  4.  
  5. return cout;
  6. }
I'm a student. If my statements seem too absolute, feel free to coat them with "In my opinion..." or "I believe...".
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 1,471
Reputation: William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of 
Solved Threads: 121
Sponsor
William Hemsworth William Hemsworth is offline Offline
Nearly a Posting Virtuoso

Re: Custom Cout<< method

 
0
  #3
Aug 1st, 2008
A few things you should add if your defining the function within the class.

friend ostream &operator<<(ostream &cout,MyClass &obj)
{
     cout << "Output members here.";

    return cout;
}
I need pageviews! most fun profile ever :)
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 401
Reputation: CoolGamer48 is on a distinguished road 
Solved Threads: 40
CoolGamer48's Avatar
CoolGamer48 CoolGamer48 is offline Offline
Posting Pro in Training

Re: Custom Cout<< method

 
0
  #4
Aug 1st, 2008
What does friend do when your outside of a class? Also, isn't only necessary to pass MyClass by reference when you're doing input?
I'm a student. If my statements seem too absolute, feel free to coat them with "In my opinion..." or "I believe...".
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 1,471
Reputation: William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of 
Solved Threads: 121
Sponsor
William Hemsworth William Hemsworth is offline Offline
Nearly a Posting Virtuoso

Re: Custom Cout<< method

 
0
  #5
Aug 1st, 2008
I dont think friend does anything outside of a class in this case, but you should pass MyClass as a reference because that way your always passing a 32-Bit variable, by passing the actual object it could reduce performance unless the object itself is also has 32-Bit´s.
Last edited by William Hemsworth; Aug 1st, 2008 at 1:43 pm.
I need pageviews! most fun profile ever :)
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 1,953
Reputation: Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of 
Solved Threads: 214
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: Custom Cout<< method

 
0
  #6
Aug 3rd, 2008
You should also be very careful about const-correctness.

Insertion operators should generally have the form:
ostream& operator << ( ostream&, const myobj& )

and extraction operators should generally have the form:
istream& operator >> ( istream&, myobj& )

There are, of course, exceptions, but you don't need to worry about them now.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 401
Reputation: CoolGamer48 is on a distinguished road 
Solved Threads: 40
CoolGamer48's Avatar
CoolGamer48 CoolGamer48 is offline Offline
Posting Pro in Training

Re: Custom Cout<< method

 
0
  #7
Aug 3rd, 2008
Why do you use constant? Does it improve performance?
I'm a student. If my statements seem too absolute, feel free to coat them with "In my opinion..." or "I believe...".
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 1,953
Reputation: Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of 
Solved Threads: 214
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: Custom Cout<< method

 
1
  #8
Aug 3rd, 2008
In practice it doesn't impact performance.

What it does is make your code more usable, more correct, and easier to read.

All you ever wanted to know about const correctness
http://www.parashift.com/c++-faq-lit...rrectness.html
http://cprogramming.com/tutorial/const_correctness.html
http://www.possibility.com/Cpp/const.html

For good measure, here's an example where it makes a difference:
  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4.  
  5. void reverse_print_const( const string& s )
  6. {
  7. cout << "const: ";
  8. for (string::reverse_iterator c = s.rbegin(); c != s.rend(); c++)
  9. cout << *c;
  10. cout << endl;
  11. }
  12.  
  13. void reverse_print_non_const( string& s )
  14. {
  15. // Take note that the function body is exactly the same as above
  16. cout << "const: ";
  17. for (string::reverse_iterator c = s.rbegin(); c != s.rend(); c++)
  18. cout << *c;
  19. cout << endl;
  20. }
  21.  
  22. int main()
  23. {
  24. string message = "Hello world!";
  25.  
  26. // Both of these work just fine
  27. reverse_print_const( message );
  28. reverse_print_non_const( message );
  29.  
  30. // But this fails for the non-const
  31. reverse_print_const( "!sseccuS" );
  32. reverse_print_non_const( "...eruliaF" ); // <-- this line won't compile!
  33.  
  34. // The reason why is that the const char* is
  35. // type-promoted to a const string temporary
  36. reverse_print_const( string( "!edud suoethgiR" ) );
  37. reverse_print_non_const( string( "...remmuB" ) ); // <-- same here
  38.  
  39. // We can test it the same with a const message
  40. const string message2( "Good-bye." );
  41. reverse_print_const( message2 );
  42. reverse_print_non_const( message2 ); // <-- same here
  43.  
  44. return 0;
  45. }
What this should make clear is that the compiler is free to use the const version of the function in all circumstances, but the non-const version is limited to pre-existing, mutable objects. Since the function never modifies the object, make it const.

The same thing is happening with the insertion operator.
  1. ostream& operator << ( ostream& outs, const foo& f )
  2. {
  3. // 'outs' is non-const, because it is modified (by writing to it)
  4. // but 'f' is const, so it can be anything, anywhere, with any
  5. // access permissions --including temporaries created by
  6. // type-promotions or returned from functions or even
  7. // generated explicitly by using an inline initializing ctor.
  8. ...
  9. return outs;
  10. }

Enjoy.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 4
Reputation: williamnz is an unknown quantity at this point 
Solved Threads: 0
williamnz williamnz is offline Offline
Newbie Poster

Re: Custom Cout<< method

 
0
  #9
Aug 4th, 2008
thanks for the good responses guys, solved.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC