Sunday, 25 August 2013

Why Can't I create an instance of a C++ class defined in the same namespace as the class definition?

Why Can't I create an instance of a C++ class defined in the same
namespace as the class definition?

A friend of mine is working on learning C++ and I was helping him along.
We are both using Visual studio 2010.
the following code gives an error:
#include <iostream>
using namespace std;
namespace Characters
{
class PlayerStats
{
public:
int HPMax, HPCurrent;
};
PlayerStats John;
John.HPMax = 1;
Characters::John.HPMax = 1;
}
the line "PlayerStats John;" seems to resolve just fine, however the lines
after ( "John.HPMax = 1;", and "Characters::John.HPMax = 1;") give the
error "Error: this declaration has no storage class or type specifier" is
it illegal to set the member variables inside the namespace in this manner
or is there something else that I am missing?

No comments:

Post a Comment