overload <<

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#ifndef FOO_H
#define FOO_H

#include <iostream>

class Foo
{
  friend std::ostream& operator<<( std::ostream&, const Foo& );
public:
  Foo::Foo();
  char* _item1;
  char* _item2;
};

#endif 


1
2
3
4
5
6
7
8
9
10
11
12
// foo.cpp
#include "Foo.h"

std::ostream& operator<< ( std::ostream& os = std::cout, const Foo& bar )
  {
    os << bar._item1 << "\t"
      << bar._item2 << std::endl;
    return os;
  }

Foo::Foo() : _item1( "item1" ), _item2( "item2" )
{}


The implementation file causes the single error:
error C2548: 'operator <<' : missing default parameter for parameter 2
for line 5.

Splain Lucy.
I removed the default value for os, and voila, it compiled just fine. Does this mean that if there is a default for one parameter, there must be a default for other remaining parameters?
Yes, but there is no point in providing default arguments for operator overloads.
Topic archived. No new replies allowed.