error: expected unqualified-id before ‘.’ token
c++, compiler-errors
Solution
You need to call the method using the scope resolution operator - `::`:
A::getInstance ();
Also, if this is meant to be a singleton, it's a very bad one. Whenever you call `getInstance()`, you'll receive a new object, and you'll run into memory leaks if you forget to delete any instances.
A singleton is usually implemented like so:
class A
{
private:
A () {}
static A* instance;
public:
static A* getInstance ()
{
if ( !instance )
instance = new A ();
return instance;
}
};
//implementation file
A* A::instance = NULL;
Problem
``` class A { private: A () {} public: static A* getInstance () { return new A (); } }; int main () { A.getInstance (); return 0; } ``` results in the error stated in the title. I do realize that if I create a variable in class A and instanciate it there and return it directly, the error will vanish. But, here I want to understand what is the meaning of this error and why can't I use it this way.