How to templatize iostream and fstream objects? This way (see the code, please) is not correct… Thanks for your help.
template <typename O>
void test(O &o)
{
o << std::showpoint << std::fixed << std::right;
o << "test";
}
int main(int argc, _TCHAR* argv[])
{
std::iostream out1; //Write into console
std::ofstream out2 ("file.txt"); //Write into file
....
test(out1);
test (out2);
return 0;
}
Your template function works perfectly for me, although your
mainfunction had some serious errors in it. After fixing your errors, this program works for me:I’m not sure why you want a template function, though. Regular polymorphism makes much more sense for this particular case.