inheriting ostream and redefinition of <<

Hi, all!
I'm having a problem that should be really simple, even if I cannot find the solution... :-)
Somehow I need to create an output stream that should work with files or pipes. That is why I created a simple class to wrap this functionality but I'm not able to make operator<< work with other stuff than characters.

The code is:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class fdostream : public std::ostream {
   protected:
     fdoutbuf buf;
   public:
     fdostream (int fd) : std::ostream(0), buf(fd) {
         rdbuf(&buf);
     }
     fdostream (ostream& out) {
	 rdbuf(out.rdbuf());
     }
     fdostream& operator<<(const int n)
	{
		cout << "Friend?" << endl;
		*this << itoa(n);
		return (*this);
	}
 };


and the idea is that it can be created with a normal ostream as a parameter or an integer that is related to a pipe.

But the definition of fdostream& operator<<(const int n) gives me the following error:

error: ISO C++ says that these are ambiguous, even though the worst conversion for the first is better than the worst conversion for the second:
note: candidate 1: fdostream& fdostream::operator<<(int)
candidate 2: std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&, _CharT) [with _CharT = char, _Traits...... blah, blah, blah...


In the case that an ostream is passed as a parameter, the output is associated with a class derived from streambuf, which code is:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
class fdoutbuf : public std::streambuf {
   protected:
     int fd;    // file descriptor
   public:
     // constructor
     fdoutbuf (int _fd) : fd(_fd) {
     }
     fdoutbuf () : fd(-1) {
     }
   protected:
     // write one character
     virtual int_type overflow (int_type c) {
         if (c != EOF) {
             char z = c;
             if (write (fd, &z, 1) != 1) {
                 return EOF;
             }
         }
         return c;
     }
     // write multiple characters
     virtual
     std::streamsize xsputn (const char* s,
                             std::streamsize num) {
         return write(fd,s,num);
     }
 };


Maybe you can find a solution to the problem or a better aproximation to what I would like to solve.

Thanks a lot in advance!

Topic archived. No new replies allowed.