#include
using namespace std;
template
class test
{
T y;
public:
test(T k) : y(k) {}
friend int a(T& x);
};
template
int a(T& x)
{
cout << x.y;
return 9;
}
template <>
class test
{
int y;
public:
test(int k) : y(k) {}
friend int a(int& x);
};
template <>
int a(int& x)
{
cout << "4";
return 0;
}
int main(int argc, char* argv[])
{
test<int> z(3);
a(z);
return 0;
}
I want to make a friend class of test class (in a real case, it was a operator<< of ofstream). But I have no idea how to define template friend function of specialized class.
Besides, the code above shows this compile error message;
error C2248: 'test::y' : cannot access private member declared in class 'test'
Question added;
Aaron McDaid works fine for me, but I was trying to overload operator<< of ofstream class.
friend ofstream& operator<< <test<int>> (ofstream& os, const test& t);
I added code above to test class and
template<>
ofstream& operator<< <test<int> > (ofstream& os, const test& t)
{
os << t.y;
return os;
}
used code above. But it looks like I cannot use os << t.y (which is int) I don't understand why this happens. The error message is
error C2027: use of undefined type 'std::basic_ofstream<_Elem,_Traits>'
JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)