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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T08:15:42+00:00 2026-05-15T08:15:42+00:00

I have written the following code: #include <iostream> using namespace std; template <class T>

  • 0

I have written the following code:

#include <iostream>
using namespace std;

template <class T>
class AA
{
  T a;

public:
AA()
{
 a = 7;
}

friend void print(const AA<T> & z);
};

template <class T>
void print(const AA<T> & z)
{
    cout<<"Print: "<<z.a<<endl;
}

void main()
{
AA<int> a;
print<int>(a);
}

And getting the following error:

error C2248: 'AA<T>::a' : cannot access private member declared in class 'AA<T>'
1>        with
1>        [
1>            T=int
1>        ]
1>        c:\users\narek\documents\visual studio 2008\projects\aaa\aaa\a.cpp(7) : see declaration of 'AA<T>::a'
1>        with
1>        [
1>            T=int
1>        ]
1>        c:\users\narek\documents\visual studio 2008\projects\aaa\aaa\a.cpp(30) : see reference to function template instantiation 'void print<int>(const AA<T> &)' being compiled
1>        with
1>        [
1>            T=int
1>        ]

What’s wrong?

P.S. I am using Visual Studio 2008.

  • 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-15T08:15:42+00:00Added an answer on May 15, 2026 at 8:15 am

    The problem is that when you do something like

    template<class T>
    class AA {friend void print(const AA<T>&);};
    

    and you instantiate AA with like this

    AA<int> a;
    

    the friend declaration will be instantiated like

    friend void print(const AA<int>&);
    

    which is a non-template function! This means the compiler will not match the friend declaration with your print function.

    The solution is basically to declare print before AA and explicitly tell the compiler that your friend declaration is talking about a template function. Like this:

    #include <iostream>
    using namespace std;
    
    //forward declare AA because print needs it
    template<class T>
    class AA;
    
    //declare print before AA to make the friend declaration
    //match with this function
    template<class T>
    void print(const AA<T> & z);
    
    template <class T>
    class AA
    {
            //the <> is needed to make sure the compiler knows we're
            //dealing with a template function here
            friend void print<>(const AA<T> & z);
    
        public:
            AA() {a = 7;}
    
        private:
            T a;
    };
    
    //implement print
    template<class T>
    void print(const AA<T> & z)
    {
        cout<<"Print: "<<z.a<<endl;
    }
    
    int main()
    {
        AA<int> a;
        print(a);
    }
    

    It is interesting to see what happens if you do not add the <> in the friend declaration. You will get a linker error. Why? Well, because the compiler cannot match the friend declaration with your template print function, it will implicitly assume a function with prototype

    void print(const AA<int>&);
    

    exists. Since I didn’t explicitly provide a template parameter to the call to print (which isn’t necessary because the compiler should be able to deduce this), the compiler will match this call with the function declared as friend. This function is nowhere implemented, hence the linker error.

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

Sidebar

Related Questions

I have written the following code: #include stdafx.h #include <iostream> using namespace std; double
please consider following code #include <iostream> using namespace std; class Digit { private: int
i have written the following code class Program { static void Main(string[] args) {
Here I have written code for finding median of two sorted arrays: #include<iostream> using
I have written the following code #include <iostream> #include <boost/asio.hpp> #include <boost/date_time/posix_time/posix_time.hpp> #include <boost/filesystem.hpp>
I am using Jersey v10 and have written the following code.Is this the right
I have the following piece of code in C++: #include <iostream> #include <fstream> #include
I have written a following code (see code comments for the question), #include<stdio.h> int
I have written following code to attach gesture recogniser to multiple imageviews. [imageview1 setUserInteractionEnabled:YES];
I am trying my hands on WPF MVVM. I have written following code in

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.