| | |
Custom Cout<< method
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Jul 2008
Posts: 4
Reputation:
Solved Threads: 0
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<<
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<<
All you're doing is defined a method of MyClass called Cout. To, use it, you would have to call it:
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):
C++ Syntax (Toggle Plain Text)
MyClass obj; 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):
C++ Syntax (Toggle Plain Text)
ostream& operator<<(ostream& cout,MyClass obj) { cout << "Output members here."; return cout; }
I'm a student. If my statements seem too absolute, feel free to coat them with "In my opinion..." or "I believe...".
•
•
Join Date: Mar 2008
Posts: 1,471
Reputation:
Solved Threads: 121
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 :)
•
•
Join Date: Mar 2008
Posts: 1,471
Reputation:
Solved Threads: 121
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 :)
You should also be very careful about const-correctness.
Insertion operators should generally have the form:
and extraction operators should generally have the form:
There are, of course, exceptions, but you don't need to worry about them now.
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.
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:
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.
Enjoy.
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:
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <string> using namespace std; void reverse_print_const( const string& s ) { cout << "const: "; for (string::reverse_iterator c = s.rbegin(); c != s.rend(); c++) cout << *c; cout << endl; } void reverse_print_non_const( string& s ) { // Take note that the function body is exactly the same as above cout << "const: "; for (string::reverse_iterator c = s.rbegin(); c != s.rend(); c++) cout << *c; cout << endl; } int main() { string message = "Hello world!"; // Both of these work just fine reverse_print_const( message ); reverse_print_non_const( message ); // But this fails for the non-const reverse_print_const( "!sseccuS" ); reverse_print_non_const( "...eruliaF" ); // <-- this line won't compile! // The reason why is that the const char* is // type-promoted to a const string temporary reverse_print_const( string( "!edud suoethgiR" ) ); reverse_print_non_const( string( "...remmuB" ) ); // <-- same here // We can test it the same with a const message const string message2( "Good-bye." ); reverse_print_const( message2 ); reverse_print_non_const( message2 ); // <-- same here return 0; }
The same thing is happening with the insertion operator.
C++ Syntax (Toggle Plain Text)
ostream& operator << ( ostream& outs, const foo& f ) { // 'outs' is non-const, because it is modified (by writing to it) // but 'f' is const, so it can be anything, anywhere, with any // access permissions --including temporaries created by // type-promotions or returned from functions or even // generated explicitly by using an inline initializing ctor. ... return outs; }
Enjoy.
![]() |
Other Threads in the C++ Forum
- Previous Thread: C command-line I/O question
- Next Thread: Syntax error ???
| Thread Tools | Search this Thread |
api application array arrays based beginner binary bmp c++ c/c++ calculator char char* class classes code compile compiler console conversion count data delete deploy dll download dynamic dynamiccharacterarray email encryption error file format forms fstream function functions game givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int java lib library linkedlist linker list loop looping loops map math matrix memory microsoft newbie news number numbertoword output pointer problem program programming project python random read recursion recursive reference rpg simple sorting string strings temperature template test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets






