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 6964723
In Process

The Archive Base Latest Questions

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

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

  • 0
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 ?

  • 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-27T15:57:38+00:00Added an answer on May 27, 2026 at 3:57 pm

    You need to include

     #include <iostream>
    

    At the time of instantiation of the function template. Perhaps you only included

     #include <iosfwd>
    

    Besides, you shouldn’t be defining (static) friend as a template: https://ideone.com/1HRlZ

    #include <iostream>
    
    template <typename> class test;
    
    template <> 
    class test<int> {
        int y; 
    public:     
        test(int k) : y(k) {}     
        friend std::ostream& operator<<(std::ostream& os, const test& t); 
    };  
    
    std::ostream& operator<< (std::ostream& os, const test<int>& t) 
    {
        return os << t.y;
    }  
    
    int main()
    {
        test<int> a(42);
        std::cout << a << std::endl;
    }
    

    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)

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

Sidebar

Related Questions

Consider the following code: template<int* a> class base {}; int main() { base<(int*)0> test;
template <class T> class Test{ public: Test(T){ } template <class U> U to() const{
check this out: template <class T> class Test: public T { public: void TestFunc()
How do I inherit from a virtual template class, in this code: // test.h
Is following design possible?: template <typename T> class Test{ public: template <typename Z> void
I have this sample code: struct A { bool test() const { return false;
#include <iostream> using namespace std; template <typename T> class test { T y; public:
Consider the following test 1 code struct A { private: class face; friend class
Try this out: template <typename T> class Base { public: int someBaseMember; }; template
template <class T> class Test { public: template<class U> void f(); //generic function template<>

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.