0
#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?

Aquarius_Girl
  • 21,790
  • 65
  • 230
  • 411

2 Answers2

2

You probably need a pointer:

static boiler* uniqueInstance;

since then you are initializing it with new here:

uniqueInstance = new boiler ();

The compiler is telling you that it cannot compare an instance of boiler with 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 NULL serves 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 to NULL by defining the appropriate operator==, but the comparison might not make sense, since NULL is often just another name for 0.

juanchopanza
  • 223,364
  • 34
  • 402
  • 480
1

You declare uniqueInstsance as a class instance:

 static boiler uniqueInstance;

but sometimes treat it as a pointer:

uniqueInstance = new boiler ();

I think you should declare it as a pointer so you can dynamically allocate the instance:

static boiler* uniqueInstance;

You'll also want to change getInstance() to return a pointer:

static boiler* getInstance() //...

Finally make sure you actually have a definition of uniqueInstance somewhere outside of the class declaration. In one spot in a .cpp file (not a header):

boiler* boiler::uniqueInstance;
Michael Burr
  • 333,147
  • 50
  • 533
  • 760