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<<

All you're doing is defined a method of MyClass called Cout. To, use it, you would have to call it:

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):

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

    return cout;
}

A few things you should add if your defining the function within the class.

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

    return cout;
}

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 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.

You should also be very careful about const-correctness.

Insertion operators should generally have the form: ostream& operator << ( ostream&, [B]const[/B] 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-lite/const-correctness.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:

#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;
  }

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.

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. :)

Comments
Good Post
This article has been dead for over six months. Start a new discussion instead.