in Education by
#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)

1 Answer

0 votes
by
This friend isn't a template, but an ordinary function: friend int a(T& x); To have a template that is also a friend, try: template friend int a(U& x); After the discussions in the comments, perhaps I should show that I intended these declarations for the test class and its specialization: template class test { T y; public: test(T k) : y(k) {} template friend int a(U& x); }; template <> class test { int y; public: test(int k) : y(k) {} template friend int a(U& x); }; A slight disadvantage is that this makes all the a functions friends of all the test classes, but that is often not a big problem.

Related questions

0 votes
    #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...
asked Feb 18, 2022 in Education by JackTerrance
0 votes
    #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...
asked Feb 16, 2022 in Education by JackTerrance
0 votes
    // merge.cpp // merges two containers into a third #include #include //for merge() using namespace std; int src1[] = { 2, 3, 4, ... , src1+5, src2, src2+3, dest); for(int j=0; j...
asked Jun 2, 2022 in Education by JackTerrance
0 votes
    I have a function value(x) which is overloaded for many types such that: double value(double x) { return x; } double ... [-Wreturn-local-addr] in the following code: Matrix33 m; m...
asked Apr 24, 2022 in Education by JackTerrance
0 votes
    #include struct A { bool f( int a ) { std::cout...
asked Feb 21, 2022 in Education by JackTerrance
0 votes
    I am working on a C++ program, which uses cURL library, which is written in plain C. When I try ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Aug 1, 2022 in Education by JackTerrance
0 votes
    Closed. This question needs to be more focused. It is not currently accepting answers. Want to improve this ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 30, 2022 in Education by JackTerrance
0 votes
    Closed. This question needs debugging details. It is not currently accepting answers. Want to improve this ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 7, 2022 in Education by JackTerrance
0 votes
    Tm trying to get the difference of 2 sets in linked list Example: Input: Set A: 1 - 2 - 3 ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 7, 2022 in Education by JackTerrance
0 votes
    Closed. This question needs debugging details. It is not currently accepting answers. Want to improve this ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 4, 2022 in Education by JackTerrance
0 votes
    I am currently working on a personal project in C++, and I'm having a lot of fun learning. I just learned some ... MainMenu::~MainMenu(){} void MainMenu::displayMenu() { std::cout...
asked May 3, 2022 in Education by JackTerrance
0 votes
    I have a c++11 type alias: using coord = std::array; Can I define the operator + for coord? ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 29, 2022 in Education by JackTerrance
0 votes
    I have a c++11 type alias: using coord = std::array; Can I define the operator + for coord? ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 26, 2022 in Education by JackTerrance
0 votes
    I would like to use ShellExecute in my C++ program to force the opening of a tiff image with ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 26, 2022 in Education by JackTerrance
0 votes
    The code below copies data from one vector into another. If the vectors are large then I guess this ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 23, 2022 in Education by JackTerrance
...