Ich habe eine Klasse:
class foo {
private:
std::string data;
public:
foo &append(const char* str, size_t n) { data.append(str,n); }
// für Debug-Ausgabe
template
friend T& operator<< (T &out, foo const &f);
// etwas anderes
};
template
T& operator<< (T &out, foo const &f) {
return out << f.data;
}
Ich möchte, dass dies mit jeder Klasse funktioniert, die den <<
Operator bereitstellt.
Dies funktioniert gut mit std::cout
wie folgt:
std::cout << fooObject;
Aber das folgende scheitert:
BOOST_AUTO_TEST_CASE( foo_append_and_output_operator )
{
// fooObject ist hier zugänglich
const char* str = "hallo";
fooObject.append(str, strlen(str));
output_test_stream output;
output << fooObject;
BOOST_CHECK( output.is_equal(str) );
}
g++
sagt mir, dass:
In function ‘T& operator<<(T&, const foo&)
[with T = boost::test_tools::output_test_stream]’:
error: invalid initialization of reference of type
‘boost::test_tools::output_test_stream&’ from expression of type
‘std::basic_ostream >’
Was passiert hier?
Ich benutze Boost 1.34.1 auf Ubuntu 8.04.