Take a look at this code. I have seen some threads on this on other forums and the answer seems to be that you have to put the operator<< inside of namespace std. I don't understand why though. In the threads that I have read I have not seen a decent explanation as to why this is. It makes no sense to me. Some others have said that this is not good to put your own code within the std namespace. But it does work in this case. I don't understand the root cause of this problem. Can anyone provide the explanation?
Try commenting out the namespace std and brackets and you'll see that it won't compile.
I agree jsmith. The problem has to do with ostream_iterator. I can also compile it okay if I write my own for loop. Strange. I read some other forums where some people said that the compiler will look for the operator<< to be defined within std because of the ostream_iterator template code.
Unfortunately I do not understand the explanations given and was hoping to find a guru who can provide something better. i wonder if this is something fixed with a later C++ std or if this is some limitation of templates. I don't get it.