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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T06:48:21+00:00 2026-05-18T06:48:21+00:00

Possible Duplicate: Making an undefined class as friend, and defining it later. I have

  • 0

Possible Duplicate:
Making an undefined class as friend, and defining it later.

I have the following code

template<typename T> 
class A
{
        class B;
        B b; 
};

int main() 
{
        return 0;       
}

The code doesn’t emit any error because A is not instantiated. Am I right?

But have a look at the second code sample

template<typename T>
class A
{
  protected:
        class a 
        {
            int x;
            int y;
          private:
            friend class b;  
        };
        template <typename U > class b
        {  
          int z;
          U y;
        };

};

int main()
{
    A<int>  a;
    return 0;
}

In this case A is instantiated but b is not. So the code shouldn’t emit any error, right? But I get a redeclaration error in msvc++2008 professional edition. It compiles fine on gcc.

What could be the reason for the error?

Sorry everyone. After reading the comments and after seeing my original code I see that I made a mistake while typing the second code sample.
I missed the definition of the constructor and the statement a *object = new a; inside the constructor. Apologies. That means my code is exactly similar to the other code posted by the other user. But now I cannot delete my question now because of the existing answers.

  • 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-18T06:48:21+00:00Added an answer on May 18, 2026 at 6:48 am

    Regarding you second code snippet …

    template<typename T>
    class A
    {
      protected:
            class a 
            {
                int x;
                int y;
              private:
                friend class b;  
            };
            template <typename U > class b
            {  
              int z;
              U y;
            };
    
    };
    
    int main()
    {
        A<int>  a;
        return 0;
    }
    

    … it is most probably invalid code, because a very similar code snippet posted by you earlier, in the SO question “Making an undefined class as friend, and defining it later”, is invalid. But I’m not sure about this.

    The difference wrt. your earlier question is only that in that other question the nested class a is used, hence, instantiated.

    However, the redeclaration error that you get with MSVC 8.0 does not necessarily mean that that compiler recognizes the code as invalid standard C++. It might be a compiler bug that just by happenchance causes it to (probably) correctly identify the code as invalid. And it might just be that the above code, not using a, is valid…

    So, it’s pretty subtle, it’s language lawyer stuff.

    Best advice is to just stay well clear of these rather dark corners of the language.

    EDIT: the earlier question was not Pavel’s (but it’s the same code); see comments.

    Cheers & hth.,

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

Sidebar

Related Questions

Possible Duplicate: C++ convert int and string to char* Hello, i am making a
Possible Duplicate: How to detect if JavaScript is disabled? I have a website which
Possible Duplicate: Learning to write a compiler I looked around trying to find out
For a class X and a QSet< X* >, how is it possible to
Possible Duplicate: Should I learn C before learning C++? As a professional (Java) programmer
Possible Duplicate: How do you give a C# Auto-Property a default value? Hi all:
Possible Duplicate: How to copy part of an array to another array in C#
Possible Duplicate: Difference between Convert.tostring() and .tostring() Hi Carrying on from this question What
Possible Duplicate: Why is my return type meaningless? Hi, I'm confused about a particular
Possible Duplicates: “const T &arg” vs. “T arg” How to pass objects to functions

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.