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

The Archive Base Latest Questions

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

namespace O { class A{}; class A; // ok typedef A K; // ok

  • 0
namespace O
{
    class A{};


    class A;   // ok
    typedef A K; // ok

    struct A;  // ok(C++11): A is a class but for references and pointer it have the same meaning
    class K;   // (1) error: K is a typedef (of a class...)
}

namespace U
{
    typedef O::A A;


    class A;      // (2) error: A is a typedef (of  a class...)
}

What is the reason(s) standard C++ don’t allow these cases (1 & 2) to compile?

  • 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-17T22:25:22+00:00Added an answer on June 17, 2026 at 10:25 pm

    You are confused or your example doesn’t show what you’re asking about. In your example code you are not trying to “forward declare a typedef” (such as thing isn’t possible or useful, see below) you are trying to redeclare an existing typedef-name (i.e. an alias for one type) as a completely different type.

    You’ve already said K is a typedef for A, then you say it’s a class K. Make your mind up. It’s can’t be both class A and class K. Both (1) and (2) fail for that same reason.

    Going through these lines of the example:

    class A;   // ok
    typedef A K; // ok
    

    Right so far.

    struct A;  // ok(C++11): A is a class but for references and pointer it have the same meaning
    

    I don’t know why you’ve said “C++11” here, this is OK in C++03 too. Classes and structs are the same kind of thing in C++. They are both “object types” and both “class types”. For a forward declaration the class-key (i.e. struct or class) is interchangeable.

    class K;   // (1) error: K is a typedef (of a class...)
    

    K has been declared as a typedef for class A, the name can’t be reused for declaring a new type in the same scope.

    [Aside: C does allow the following, because struct names and typedef names are in separate namespaces:

    struct A { };
    typedef struct A K;  // N.B. need "struct A" not just "A"
    struct K { }; 
    

    But now there are two different types called struct K and K, which are unrelated. Doing this would be confusing and pretty dumb.]

    But from your comments maybe that’s not what you’re actually trying to do anyway.

    Based on your comments maybe your broken examples are misleading and what you really want to do is:

    typedef class A K;   // forward declare A as class and declare K as typedef for it
    

    This declares a typedef, for a type which is not defined yet.

    It would be useless to forward-declare a typedef, you couldn’t do anything with it because you wouldn’t know what kind of type it was a typedef for, and there is very little you can do in C++ without knowing something about a type. Without knowing if it’s an object type, reference type or function type all you can realistically do is declare another typedef for it!

    Consider:

    typedef K;   // declares K to be a typedef
    
    K* f();      // function returning pointer to K
    void g(K*);  // function taking pointer to K
    

    I think you’re saying you want that to be valid, so do you expect this to work?

    K* k = f();
    g(k);
    

    That should work, right? You don’t need to know the type of K because you only pass around pointers to it, right? Wrong. What if you later define K like this:

    typedef int& K;
    

    Now f has the signature int&* f() which is invalid. You have to know what a typedef is a typedef for, so its declaration has to say what it is not just forward-declare it as a name.

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

Sidebar

Related Questions

I have this code: namespace js0n { struct json { typedef int json_object; json(){}
I have the below code in stdafx.h. using namespace std; typedef struct { DWORD
Hello I have the following code namespace ConsoleApplication2 { class Program { static void
Please have a look at the following code namespace Funny { class QuesionsAndAnswers {
I have a typedef inside a class and I would like to overload the
I have a file that looks like this: namespace myName { typedef HRESULT (*PFN_HANDLE)(myName::myStruct);
#include <iostream> #include <string> using namespace std; struct Uid { typedef int type; };
I have a vector of smart ptr of my class Foo: struct Foo {
#include <iostream> using namespace std; struct Term; struct Node; typedef Term* termPtr; typedef Node*
Consider the following program: #include <iostream> #include <algorithm> using namespace std; template<class T> struct

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.