Pregunta

I am currently working on a school project that uses the old VS2008 with essentially little to no error assistance. Anyways, I am trying to learn about Singeltons and was trying to create as basic as it goes. But suddenly from nowhere I am getting errors. Am I that blind or what am I doing wrong? Error:

1>.\Source\Singelton.cpp(3) : error C2065: 'NULL' : undeclared identifier
1>.\Source\Singelton.cpp(12) : error C2065: 'NULL' : undeclared identifier
1>.\Source\Singelton.cpp(21) : error C2065: 'NULL' : undeclared identifier
1>.\Source\Singelton.cpp(26) : error C2065: 'value' : undeclared identifier
1>.\Source\Singelton.cpp(30) : error C2065: 'value' : undeclared identifier

Complete code: header:

#ifndef __SINGELTON_H__
#define __SINGELTON_H__


class Singelton
{
private:
    static Singelton* instance;
    Singelton();
    int value;
public:
    ~Singelton();
    int get_value();
    void set_value(int v);
    static Singelton* getInstance();

};
#endif

.cpp

#include "Singelton.h"

Singelton* Singelton::instance = NULL;

Singelton::Singelton()
{
    value=0;
}

Singelton* Singelton::getInstance()
{
    if (instance == NULL)
    {
        instance = new Singelton();
    }
    return instance;
}

Singelton::~Singelton()
{
    instance = NULL;
}

int get_value()
{
    return value;
}
void set_value(int v)
{
    value=v;
}
¿Fue útil?

Solución

error C2065: 'NULL' : undeclared identifier

You haven't included <cstddef>, which defines NULL.

error C2065: 'value' : undeclared identifier

You forgot to include the class name when you defined the member functions, so instead declared new non-member functions:

int Singelton::get_value()
{// ^^^^^^^^^^^
    return value;
}

You should also spell "singleton" properly and avoid reserved names like __SINGELTON_H__. Hopefully, you'll soon be taught why singletons are a bad idea in general, and doubly so in C++. Have a look here, in case your teacher forgets to cover that.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top