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

  • Home
  • SEARCH
  • 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

Ask A Question

Stats

  • Questions 446k
  • Answers 446k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer The second approach seems reasonable and more pleasing. It comes… May 15, 2026 at 7:18 pm
  • Editorial Team
    Editorial Team added an answer Sorry all, but there was some other problem. The code… May 15, 2026 at 7:18 pm
  • Editorial Team
    Editorial Team added an answer you can use \042. awk 'BEGIN{RS="\042"}{}' file or awk -vRS='"'… May 15, 2026 at 7:18 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.