Why does the `operator<<` not work on member pointing to derived type? : <p>I have a class <code>Base</code> with a member pointing to a derived type <code>Derv</code>:</p>

<pre><code>class Derv;

class Base
{
protected:
    std::vector&lt;std::shared_ptr&lt;Derv&gt;&gt; opnds;
    ...
}
</code></pre>

<p>The derived class looks like this:</p>

<pre><code>#include "Base.h"

class Derv:
    public Base
{
...
}
</code></pre>

<p>Now I want to <code>cout</code> all <code>Base</code> and derived types by serializing the items from <code>opnds</code>. I read that something like the following is the standard approach for that (since I might want to override the serialization from other derived classes). I included into <code>Base.h</code>:</p>

<pre><code>friend std::ostream &amp;operator&lt;&lt;(std::ostream &amp;os, math_struct const &amp;m);
virtual void serialize(std::ostream&amp; os) const;
</code></pre>

<p>In <code>Base.cpp</code> I implemented:</p>

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

void Base::serialize(std::ostream&amp; os) const
{
    for (std::size_t i = 0; i &lt; this-&gt;opnds.size(); ++i) {
        os &lt;&lt; ", " &lt;&lt; *this-&gt;opnds[i]; // Error: no operator "&lt;&lt;" matches these operands
    }
}

std::ostream&amp; operator&lt;&lt;(std::ostream &amp;os, math_struct const &amp;m) {
    m.serialize(os);
    return os;
}
</code></pre>

<p>But the recursive application of <code>&lt;&lt;</code> in <code>Base::serialize</code> serialize doesn't work. It seems to have to do with the derived class being referenced in the base class member. An earlier version where I head <code>std::vector&lt;std::shared_ptr&lt;Base&gt;&gt; opnds</code> worked fine.</p>

<p>I'm new to C++, so probably I got something basic wrong...</p>
