c++ - 错误 : no match for ‘operator==’ in ‘boiler::uniqueInstance == 0l’

标签 c++ design-patterns

#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;
    }
};

以上代码返回标题中所述的错误。

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&)

为什么?我们不能将“对象”与 NULL 进行比较吗?是否存在一些语法问题?

最佳答案

你可能需要一个指针:

static boiler* uniqueInstance;

从那时起,您将在此处使用 new 对其进行初始化:

uniqueInstance = new boiler ();

编译器告诉您它无法将 boiler 的实例与 int(实际上是 long)进行比较。这种比较是不存在的。指针可以与整数类型进行比较,这允许与 0 进行比较。

此处与 NULL 的比较用作检查您的指针是否已初始化的方法。如何对实例执行此操作并不明显,没有无效或未初始化实例的概念。您可以通过定义适当的 operator== 将对象与 NULL 进行比较,但这种比较可能没有意义,因为 NULL 通常只是另一个对象0 的名称。

关于c++ - 错误 : no match for ‘operator==’ in ‘boiler::uniqueInstance == 0l’ ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10443457/

相关文章:

c++ - 定义麻烦。 C++

c++ - Cmake 编译器问题

c++ - 标准算法库中的哪些算法进行分配,有没有办法指定这种分配是如何发生的?

c# - 在 View 之间传递数据的最佳方式是什么?

multithreading - 我应该使用哪种 boost 多线程设计模式?

c++ - 无法打开包含文件 : 'iProxyTrans.h' - old Directshow project?

如果 blitted 到特定表面,则 C++ SDL 冲突

C# - 遍历谓词的模式

design-patterns - MVVM是哪种类型的设计模式?

database - 表示对象的表单的自定义字段