c++ object creation error after constructor

Hi,

i have a class called A with a constructor. In the main program I have the following code:

int main{
A A(10);

A obj;

...
...
}

I get error in the line of A obj; error: 'obj' was not declared in this scope

The program works by either removing A A(10) or A obj and their related codes.

Can anyone tell me what is wrong?

Thanks!!
The variable name shadows the type name.

1
2
3
4
5
// Creates an object of type A, named A.
A A(10);

// The compiler gets confused. A is a variable so what is obj doing after it?
A obj;


You could work around this by explicitly specify the namespace of class A.
If A is defined in the global namespace you can write ::A obj;
but probably a better solution is to avoid using the same names for variables and types in the first place.
Last edited on
Topic archived. No new replies allowed.