Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6948597
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T13:50:37+00:00 2026-05-27T13:50:37+00:00

#include <iostream> using namespace std; template <typename T> class test { T y; public:

  • 0
#include <iostream>
using namespace std;
template <typename T>
class test
{
    T y;

public:
    test(T k) : y(k) {}
    friend int a(T& x);
};

template <typename T>
int a(T& x)
{
    cout << x.y;
    return 9;
}

template <>
class test<int>
{
    int y;
public:
    test(int k) : y(k) {}
    friend int a(int& x);
};

template <>
int a<int>(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<int>& t);

I added code above to test class and

template<>
ofstream& operator<< <test<int> > (ofstream& os, const test<int>& 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>’

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T13:50:38+00:00Added an answer on May 27, 2026 at 1:50 pm

    (Update: Here’s a fully tested version on http://ideone.com/3KGU4. For the Additional question, see http://ideone.com/w0dLo)

    There is a difference between ordinary overloaded functions and template functions. For example, without any reference to templates a developer can declare:

    void f(int x);
    void f(char *x);
    

    Alternatively, a developer could use templates,

    template <class T> void f(T x);
    

    A major difference between them is that with ordinary functions, you must decide on a fixed set of allowed parameters in advance, and you must provide an implementation for each one. With templates, you can be more flexible.

    Later in your program, it is clear that you want a to be a template function, not simply an (overloaded) ordinary function. But when the compiler first sees mention of a (around line 10), it looks like it is declaring an ordinary function. To resolve this, you must take two steps. You must declare as soon as possible that a is a template function, so your first line should be:

    template <typename T> int a(T& x);
    

    Then you must declare the relevant friendship. If T is int, then a takes a parameter of test<int>&, not int&. Therefore the two friend lines should be replaced with:

    friend int a<test<T> >( test<T> & x); // around line 10
    friend int a<test<int> >( test<int> & x); // around line 27
    

    and the specialization of a should be:

    template <>
    int a< test<int> >(test<int>& ) // around line 30
    

    The Additional Question

    Use ostream instead of ofstream (or else include #include <fstream> if you will output only to files and not to cout). In my answer, operator << is not a template, but is a normal overloaded function. I’m not sure it’s possible to have operator<< as a template. Also, I defined the operator at the place where it is declared and declared as a friend. To be honest, I think there are other, maybe better, ways but this worked for me.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

#include<iostream> using namespace std; class A { int a; int b; public: void eat()
#include <iostream> using namespace std; class Base { public: Base(){cout <<Base<<endl;} virtual ~Base(){cout<<~Base<<endl;} virtual
#include <iostream> using namespace std; struct testarray{ int element; public: testarray(int a):element(a){} }; class
Here the code #include <iostream> #include <conio.h> using namespace std; template <typename T> class
I have template function compare defined as below. #include<iostream> using namespace std; template<typename T>
#include <iostream> using namespace std; int main() { cout << !!!Hello World!!! << endl;
Given the code: #include <iostream> using namespace std; template <typename T> T my_max (const
#include <iostream> #include <vector> using namespace std; template <typename nameOfTheVariableTypeA, typename nameOfTheVariableTypeB> nameOfTheVariableTypeB functionX
#include <iostream> using namespace std; int main() { double u = 0; double w
#include <iostream> using namespace std; // This first class contains a vector and a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.