This code:
1 2 3 4
|
class Foo
{
//...
} bar;
|
Is exactly the same as this code:
1 2 3 4 5 6
|
class Foo
{
//...
};
Foo bar;
|
What you have here, is you have a class named
a
, but then you are also creating an object named
a
. It'd be like doing this:
This is perfectly legal. However you now have a problem where this 'a' name is the name of an
object which takes precedent of the name of the class.
So later, when you do this:
The compiler sees that
a
and it thinks you mean the
object a and not the classname a. So it'll give you a compiler error because that makes no sense.
However if you prefix that line with the
class
keyword... you are clarifying that you mean the class name and not the object name.
|
class a b(1); // now it understands you want the 'a' class, not the 'a' object.
|
Of course, a better solution would be to name your object something else so it doesn't conflict with the class name:
1 2 3 4 5 6 7 8
|
class a
{
//...
} c(2); // <- don't name this 'a'
int main()
{
a b(1); // then this is fine
|