Problem with overriding "operator<<" in class derived from "ostream"
<p>I want to make a class based on "ostream" that does some auto-formatting to generate comma- or tab-separated-value files. My idea was to override "operator&lt;&lt;" to have it insert a separator before each value (except at the beginning and end of a line), and also to quote strings before writing them. Within the overriding "operator&lt;&lt;" method, I wanted to call the method of the base class, but I can't get it to work right.</p>

<p>Here's an example (compiles with g++ 4.3.3):</p>

<pre><code>#include &lt;iostream&gt;
#include &lt;ostream&gt;
#include &lt;string&gt;

using namespace std;

class MyStream: public ostream
{
public:
  MyStream(ostream&amp; out): ostream(out.rdbuf()) {}

  template &lt;typename T&gt; MyStream&amp; operator&lt;&lt;(T value)
  {
    ostream::operator&lt;&lt;('+');
    ostream::operator&lt;&lt;(value);
    ostream::operator&lt;&lt;('+');
    return *this;
  }
};

template&lt;&gt; MyStream&amp; MyStream::operator&lt;&lt; &lt;string&gt;(string value)
{
  ostream::operator&lt;&lt;('*');
  ostream::write(value.c_str(), value.size()); // ostream::operator&lt;&lt;(value);
  ostream::operator&lt;&lt;('*');
  return *this;
}

int main()
{
  MyStream mystr(cout);
  mystr &lt;&lt; 10;
  cout &lt;&lt; endl;
  mystr &lt;&lt; "foo";
  cout &lt;&lt; endl;
  mystr &lt;&lt; string("test");
  cout &lt;&lt; endl;
  return 0;
}
</code></pre>

<p>The two "operator&lt;&lt;" methods (template and specialization) are there to handle strings differently than everything else. But:</p>

<ol>
<li>The characters ('+'/'*') are printed as numbers and not characters.</li>
<li>The C-String "foo" prints as a memory address (I think).</li>
<li>If the "write" line is exchanged with the commented part, the compiler complains that there's "no matching function for call to 'MyStream::operator&lt;&lt;(std::string&amp;)'", even though I thought I was explicitly calling the base class method.</li>
</ol>

<p>What am I doing wrong? Any help greatly appreciated.</p>
