template <>
class test<int> {
int y;
public:
test(int k) : y(k) {}
friend ofstream& operator<< <test<int>> (ofstream& os, const test<int>& t);
};
template<>
ofstream& operator<< <test<int> > (ofstream& os, const test<int>& t)
{
os << t.y;
return os;
}
The code above is specialized template class of test in a int version. I am trying to overload ofstream operator<< function. But it shows error message;
C2027: use of undefined type ‘std::basic_ofstream<_Elem,_Traits>’
Besides, the same method works on a ordinary function (not ofstream operator<< but the function that I make) Is there anyway to us operator<< function of ofstream in a specialized template class ?
You need to include
At the time of instantiation of the function template. Perhaps you only included
Besides, you shouldn’t be defining (static) friend as a template: https://ideone.com/1HRlZ
Note that it isn’t a good idea to have ‘using namespace std’ in your header file, which is why I removed it from the sample. (It might cause conflicts for users of your header file when they include your header)