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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T22:17:32+00:00 2026-06-14T22:17:32+00:00

I have two simple class that I made just to understand how friend class

  • 0

I have two simple class that I made just to understand how friend class works. I am confused as to why this doesn’t compile and also would the Linear class have access to the struct inside Queues class?

Linear.h

template<typename k, typename v >
 class Linear
 {
    public:
    //Can I create a instance of Queues here if the code did compile? 

    private:
  };

Linear.cpp

#include "Linear.h" 

Queues.h

 #include "Linear.h"

 template<typename k, typename v >
 class Linear;

  template<typename x>
  class Queues
  {
    public:

    private:
        struct Nodes{
            int n;
        };
    //Does this mean I am giving Linear class access to all of my Queues class variable or    is it the opposite ? 
    friend class Linear<k,v>;
    };

Queues.cpp

  #include"Queues.h" 

My errors are

Queues.h:15: error: `k' was not declared in this scope
Queues.h:15: error: `v' was not declared in this scope
Queues.h:15: error: template argument 1 is invalid
Queues.h:15: error: template argument 2 is invalid
Queues.h:15: error: friend declaration does not name a class or function
  • 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-06-14T22:17:33+00:00Added an answer on June 14, 2026 at 10:17 pm

    To answer your initial question:

    the friend keyword inside a class let the friend function or class access otherwise private field of the class where the friend constraint is declared.
    See this page for a thorough explanation of this language feature.

    Regarding the compilation errors in your code:
    In this line:

    friend class Linear<k,v>;
    

    ask yourself, what is k, where is it defined? Same for v.

    Basically, a template is not a class, it’s a syntactic construct letting you define a “class of class”, meaning that for the template:

    template <typename T>
    class C { /* ... */ };
    

    you don’t have yet a class, but something that will let you define class if you provide it a proper typename. Within the template the typename T is defined, and can be used in place as if it were a real type.

    In the following code snippets:

    template <typename U> 
    class C2 {
       C<U> x;
       /* ... */
    };
    

    you define another template, which when instantiated with a given typename will contain an instance of the template C with the same typename. The typename U in the line C<U> x; of the code above, is defined by the including template. However, in your code, k and v don’t have such a definition, either in the template where they are being used, or at the top level.

    In the same spirit, the following template:

    template <typename U> 
    class C2 {
       friend class C<U>;
       /* ... */
    };
    

    when instantiated, will have the instance of the class template C (again with the same parameter U) as a friend. As far as I know, It is not possible to have class template instances be friend with a given class, for all possible parameters combinations (The C++ language doesn’t support existential types yet).

    You could, for instance, write something like:

    template<typename x>
    class Queues
    {
      public:
    
      private:
         struct Nodes{
            int x;
         };
    
      friend class Linear<x,x>;
    };
    

    to restrict the friendliness of Linear to only the instance of that template with x and x, or something like this:

    template<typename x,typename k, typename v>
    class Queues
    {
      public:
    
      private:
         struct Nodes{
            int x;
         };
    
      friend class Linear<k,v>;
    };
    

    If you want to allow k and v to be defined ad lib.

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

Sidebar

Related Questions

I have these two simple files that define a C++ class with a tryME
I have a simple class that handles the connection being made between a client
I have a simple Fluent NHibernate model with two related classes: public class Applicant
I have a very simple scenario, using NHibernate: one abstract base class animal; two
I have two simple submit buttons on my form, which work just fine: <button
I have two very simple, identical UITableViews in my app that are populated with
I have two functions that return simple strings. Both are registered. $.views.helpers({ parseDate: function
I have following two simple POJOs: class Person { String name Address address; //and
I've just made the unfortunate (for my app at least) discovery that two methods
I have a simple two-field form that stores its data in the database. For

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.