Hey. Is it possible to overload operator<< for primitive types? Fx lets say that I want to write a std::endl each time want to write a int. Can I overload operator<< for int, so that it automatic puts a std::endl to the output? I have tried with this,
std::ostream& operator<<(std::ostream& strm, int & i) { strm << i << std::endl; return strm; }
but it doesn’t work. I cant recall the compiler error message, but I think that I’m getting operator overloading all wrong any ways. I try to call the above overloaded operator<< in this way,
int main() { int i = 2; std::out<<'Here is an int ' << i; return 0; }
But it doesn’t work at all. Maybe I can’t overload POD types?
As zabzonk said, the standard library provides an (ostream&, int) overload so you can’t define another.
To simulate what you were doing (though it is completely pointless in its present form 🙂 :