2

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

#include <iostream>
#include <string>


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

    // This works
    std::cout << message;

    // Compiler error
    std::cout.operator <<(message);
}

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

Solved

As Tomalak and Prasoon indicated I needed to call this function:

std::ostream& operator<<(std::ostream&, const std::string&);

So the following sample will work:

#include <iostream>
#include <string>


int main()
{
    std::string message = "Hello World!\n";
    operator<<(std::cout, message);
}

Concerning my original goal (to use for_each to print a vector of strings): It seems like it's better to use std::copy with std::ostream_iterator as illustrated here: How do I use for_each to output to cout?

Community
  • 1
  • 1
StackedCrooked
  • 34,653
  • 44
  • 154
  • 278
  • did you try to compile my code? I think it will be fun if you choose my answer correct ;) Cheers! – Alan Turing Jan 21 '11 at 20:01
  • You question is incorrect, the function you mentioned in code does not exists, so it will never compile. You are calling different fountion operator, that in first <<, so it is not compile, because no such a friend function, nor std << overloadable operator accepting your arguments... – Alan Turing Jan 21 '11 at 20:08
  • The most fun is that the right answer is not marked as correct ;) Propbably , i will mark it ;) – Alan Turing Jan 21 '11 at 20:35

2 Answers2

13

You're looking for std::ostream& operator<<(std::ostream&, const std::string&);. It's a free function.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
  • +1 for being correct. In particular, for not stating that `std::cout << message` would be equivalent to any particular syntactic form of function call (which is too much of a simplification to be true. What if you have a `operator<<` declared locally? It will hide the non-local name!). – Johannes Schaub - litb Jan 21 '11 at 20:27
  • @Johannes : `What if you have a operator<< declared locally? It will hide the non-local name!)` Considering the given code in the opening post that(quote) seems unlikely. – Prasoon Saurav Jan 21 '11 at 20:42
  • @Artur: Since your answer has had 4 downvotes, not sure where you get off downvoting my accepted answer, which otherwise has none! What's incomplete about it? – Lightness Races in Orbit Jan 21 '11 at 20:47
  • 1
    @Prasoon true. But there's danger that @Stacked doesn't know about that. If I were him, I would think that they are equivalent when someone tells me "std::cout << message; would be equivalent to operator << (std::cout, message)". – Johannes Schaub - litb Jan 21 '11 at 21:03
  • @Johannes Schaub: Don't worry I'm in no immediate danger :) – StackedCrooked Jan 21 '11 at 21:08
  • 2
    @Tomalak: I think Artur is a troll; he's trying to justify using undefined behaviour in his comments in [this question](http://stackoverflow.com/q/4754884/10320). – dreamlax Jan 22 '11 at 00:23
  • 1
    @Tomalak: lol, anyway to restore the balance of the universe I have given you an upvote. – dreamlax Jan 22 '11 at 00:31
13

std::cout << message; would be equivalent to operator << (std::cout, message) not what you've written.

operator << has been defined as a free function and not as a member function

std::ostream& operator<<(std::ostream& out, const std::string& s) { ... }

Prasoon Saurav
  • 91,295
  • 49
  • 239
  • 345