#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;
}
};
Приведенный выше код возвращает ошибку, указанную в названии.ошибка: нет соответствия для 'operator ==' in 'boiler :: uniqueInstance == 0l'
[email protected]:~> 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&)
Почему? Разве мы не можем сравнить «объект» с NULL? Существуют ли синтаксические проблемы?
Если вы хотите использовать '==' с вашим собственным классом, вам понадобится 'operator ==' перегрузка. – chris
Может помочь вам: http://stackoverflow.com/questions/1008019/c-singleton-design-pattern/1008289#1008289 – Muggen
Кроме того, это также полезно понять, почему C++ не является Java: http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list – Muggen