#include <iostream>
using namespace std;
class boiler
{
private:
static boiler uniqueInstance;
bool boilerEmpty;
bool mixtureBoiled;
boiler()
{
boilerEmpty = true;
mixtureBoiled = false;
}
public:
static boiler getInstance()
{
if(uniqueInstance == NULL)
{
uniqueInstance = new boiler();
}
return uniqueInstance;
}
};
The above code returns the error stated in the title.
anisha@linux-y3pi:~> g++ -Wall test.cpp
test.cpp: In static member function ‘static boiler boiler::getInstance()’:
test.cpp:22:26: error: no match for ‘operator==’ in ‘boiler::uniqueInstance == 0l’
test.cpp:24:34: error: no match for ‘operator=’ in ‘boiler::uniqueInstance = (operator new(2u), (<statement>, ((boiler*)<anonymous>)))’
test.cpp:5:1: note: candidate is: boiler& boiler::operator=(const boiler&)
why? Can’t we compare an “object” to a NULL? Are there some syntax problems?
You probably need a pointer:
since then you are initializing it with
newhere:The compiler is telling you that it cannot compare an instance of
boilerwith an int (a long actually). This comparison doesn’t exist. Pointers can be compared to integral types, which allows the comparison to 0.Here the comparison to
NULLserves as a means to check whether your pointer has been initialized already. It is not obvious how to do this with instances, there is no concept of invalid or uninitialized instance. You can compare an object toNULLby defining the appropriateoperator==, but the comparison might not make sense, sinceNULLis often just another name for0.