Difference between `cout << x` and `cout.operator <<(x)`?
<p>I've been trying to use for_each to print a vector of strings to cout. However, when formulating the statement I found that <code>std::ostream::operator&lt;&lt;(const std::string &amp;);</code> is not defined leading to compiler errors. The following code illustrates the problem:</p>

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


int main()
{
    std::string message = "Hello World!\n";

    // This works
    std::cout &lt;&lt; message;

    // Compiler error
    std::cout.operator &lt;&lt;(message);
}
</code></pre>

<p>I thought that both statements should appear identical to the compiler. Apparently they are not. So what is the difference then?</p>

<h3>Solved</h3>

<p>As Tomalak and Prasoon indicated I needed to call this function:</p>

<pre><code>std::ostream&amp; operator&lt;&lt;(std::ostream&amp;, const std::string&amp;);
</code></pre>

<p>So the following sample will work:</p>

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


int main()
{
    std::string message = "Hello World!\n";
    operator&lt;&lt;(std::cout, message);
}
</code></pre>

<p>Concerning my original goal (to use for_each to print a vector of strings): It seems like it's better to use <code>std::copy</code> with <code>std::ostream_iterator</code> as illustrated here: <a href="http://stackoverflow.com/questions/4153110/how-do-i-use-for-each-to-output-to-cout">How do I use for_each to output to cout?</a></p>
