Re: [C++] std::ostream::rdbuf()
From: Martin Henne (martin.henne_at_web.de)
Date: 10/25/03
- Next message: Huub: "Re: reading systemtime in microseconds"
- Previous message: Jerry Coffin: "Re: Nasty code...but please critique it anyway :-)"
- In reply to: Mike Wahler: "Re: [C++] std::ostream::rdbuf()"
- Next in thread: Robert W Hand: "Re: [C++] std::ostream::rdbuf()"
- Reply: Robert W Hand: "Re: [C++] std::ostream::rdbuf()"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Sat, 25 Oct 2003 20:00:26 +0200
Mike Wahler wrote:
> Show us exactly what you tried, and define 'doesn't work'.
This is the compile error:
no match for `MyOstream& << <unknown type>' operator
Below is the code (as posted by Stefan before _plus_
the one line in main() that causes this error).
// Code example
#include <iostream>
class setimportance {
int imp;
public:
setimportance(int i):imp(i) {}
int get_importance() const {return imp;}
};
class MyOstream {
std::ostream & out;
int verbosity;
int importance;
public:
MyOstream(std::ostream& os)
: out(os),
verbosity(0),
importance(0)
{
}
void set_verbosity(int v) {
verbosity=v;
}
friend MyOstream & operator << (MyOstream & o, setimportance & imp);
template <typename T>
MyOstream & operator << (T const & item) {
if (importance<verbosity)
out << item;
return *this;
}
};
template <>
MyOstream & MyOstream::operator << (setimportance const & imp) {
importance=imp.get_importance();
return *this;
}
int main() {
MyOstream out (std::cout);
for(int v=1;v<3;++v) {
out.set_verbosity(v);
std::cout << "verbosity set to " << v << std::endl;
out << setimportance(0) << "Hah ... I dont care about verbosity!\n";
out << setimportance(1) << "Uh ... but I have to!\n";
out << "This endl causes a compilation error" << std::endl;
}
}
// End of Code
-- martin dot henne at web dot de
- Next message: Huub: "Re: reading systemtime in microseconds"
- Previous message: Jerry Coffin: "Re: Nasty code...but please critique it anyway :-)"
- In reply to: Mike Wahler: "Re: [C++] std::ostream::rdbuf()"
- Next in thread: Robert W Hand: "Re: [C++] std::ostream::rdbuf()"
- Reply: Robert W Hand: "Re: [C++] std::ostream::rdbuf()"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|