In c++ the scope of the declaration go from top to bottom. So if you include first Test.h
and after that <iostream>
the friend declaration has does not know about the type std::ostream
.
The solution:
Test.h:
#include <iostream>
class Test
{
private:
int size;
public:
friend std::ostream& operator<< (std::ostream &out,const Test& test);
};
Test.cpp:
#include "Test.h"
std::ostream& operator<< (std::ostream& out,const Test& test)
{
out << test.size;
return (*out);
}
Note that the #include <iostream>
has been moved from Test.cpp
to Test.h
and the argument of the global operator <<
takes const Test& test
. The const makes the operator work for rvalues.