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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T03:20:47+00:00 2026-06-14T03:20:47+00:00

I happen to write a code like this: class a { public: a() {}

  • 0

I happen to write a code like this:

class a
{
    public:
        a() {}
};


int main()
{
    a *a = new a;        // line 10
    a a;                 // line 11
    return 0;
}

g++ errors out:

2.c: In function ‘int main()’:
2.c:10:16: error: expected type-specifier before ‘a’
2.c:10:16: error: cannot convert ‘int*’ to ‘a*’ in initialization
2.c:10:16: error: expected ‘,’ or ‘;’ before ‘a’
2.c:11:7: error: expected ‘;’ before ‘a’

I found that, if I change “a *a” to “a *b” at line 10, then g++ is happy, here is the good code:

class a
{
    public:
        a() {}
};


int main()
{
    a *b = new a;
    a a;
    return 0;
}

I am confused, not sure why the original code does not compile and how the “fix” works.

Any idea?

  • 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-14T03:20:48+00:00Added an answer on June 14, 2026 at 3:20 am

    See Vaughn’s answer for details. However, you can fix this problem if you specify that you want to use the class and not the variable:

    class a
    {
        public:
            a() {}
    };
    
    
    int main()
    {
        a *a = new class a;
        return 0;
    }
    

    or

    int main()
    {
        class a a; // although the class word isn't needed here
        return 0;
    }
    

    Explanation

    Back in the days of C structs were put in their own namespace. In C++ something similar happens, however, the class names are available outside of their namespace, as long as there is no local function or variable with the same name.

    If you happen to use the same name for both a class/struct A and a variable/function A you have to use the struct/class keyword, because the compiler interprets all following occurrences of A as the variable/function and not the struct/class.

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

Sidebar

Related Questions

I commonly write code that is along these lines: public class MyClass { static
I have a class like this: class Inner; class Cont { public: Cont(); virtual
Why does this happen? I am getting a string from another class to compare
What bad things happen at the moment document.write() is invoked? I've heard bits and
I happen to come across a Java code at my work place. Here's the
What exactly needs to happen in order to get detailed SQL logs out of
I see a lot of legacy .Net 1.1-style code at work like in example
Currently I'm attempting to write my own wxObject, and I would like for the
This pattern comes up very frequently in my code: x= x== 0? 1: x;
I am developing in python a file class that can read and write a

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.