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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T04:07:40+00:00 2026-05-23T04:07:40+00:00

Trying to compile: class AnonymousClass { public: AnonymousClass(int x) { } }; int main()

  • 0

Trying to compile:

class AnonymousClass
{
public:
    AnonymousClass(int x)
    {
    }
};


int main()
{
    int x;
    AnonymousClass(x);
    return 0;
} 

generates errors from MSVC:

foo.cpp(13) : error C2371: 'x' : redefinition; different basic types
    foo.cpp(12) : see declaration of 'x'
foo.cpp(13) : error C2512: 'AnonymousClass' : no appropriate default constructor available

g++’s error messages are similar:

foo.cpp: In function ‘int main()’:
foo.cpp:13: error: conflicting declaration ‘AnonymousClass x’
foo.cpp:12: error: ‘x’ has a previous declaration as ‘int x’
foo.cpp:12: warning: unused variable ‘x’

It’s easily fixable by giving the AnonymousClass object an explicit name, but what’s going on here and why? I presume that this is more declaration syntax weirdness (like the cases described in Q10.2 and Q10.21 of the comp.lang.C++ FAQ), but I’m not familiar with this one.

  • 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-23T04:07:40+00:00Added an answer on May 23, 2026 at 4:07 am
    AnonymousClass(x);
    

    It defines a variable x of type AnonymousClass. That is why you’re getting redefinition error, because x is already declared as int.

    The parentheses are superfluous. You can add even more braces like:

    AnonymousClass(x);
    AnonymousClass((x));
    AnonymousClass(((x)));
    AnonymousClass((((x))));
    //and so on
    

    All of them are same as:

    AnonymousClass x;
    

    Demo: http://www.ideone.com/QnRKH


    You can use the syntax A(x) to create anonymous object, especially when calling a function:

    int x = 10;
    f(A(x));        //1 - () is needed
    f(A((((x)))));  //2 - extra () are superfluous
    

    Both line 1 and 2 call a function f passing an object of type A :

    • http://www.ideone.com/ofbpR

    But again, the extra parentheses are still superfluous at line 2.

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

Sidebar

Related Questions

When trying to compile my class I get an error: The constant 'NamespaceName.ClassName.CONST_NAME' cannot
Im trying to compile a simple class library project,its from Source Safe. However,when i
Lets say I have this amputated Person class: class Person { public int Age
I'm trying to compile my class along with a provided .jar file which contains
I am trying to compile the following code on Linux using gcc 4.2: #include
I'm trying to avoid a dynamic type in my lambda expression for grouping a
I am new to Linq and trying to get a handle on how to
I've started writing a class to model a matrix and the compiler gives me
I have two classes Hello1 and Hello, and I'm calling class Hello1 constructor within
I'm currently working on a port of a jEdit plugin to write all code

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.