【问题标题】:sfml compile error: NULL was not declared in this scopesfml 编译错误:NULL 未在此范围内声明
【发布时间】:2013-03-05 15:37:06
【问题描述】:

我正在运行 Linux Mint。版本信息如下:

$ cat /etc/*-release
DISTRIB_ID=LinuxMint
DISTRIB_RELEASE=12
DISTRIB_CODENAME=lisa
DISTRIB_DESCRIPTION="Linux Mint 12 Lisa"

我正在尝试为使用 sfml 的 C++ 程序编译和链接文件。尝试编译文件时,出现以下错误:

$g++ -c lineTest.cpp Rasterizer.cpp simpleCanvas.cpp
In file included from /usr/local/include/SFML/System/Resource.hpp:211:0,
             from /usr/local/include/SFML/Graphics/Image.hpp:31,
             from simpleCanvas.h:13,
             from simpleCanvas.cpp:9:
/usr/local/include/SFML/System/ResourcePtr.inl: 
In constructor ‘sf::ResourcePtr< <template-parameter-1-1> >::ResourcePtr()’:
/usr/local/include/SFML/System/ResourcePtr.inl:31:12: 
error: ‘NULL’ was not declared in this scope
/usr/local/include/SFML/System/ResourcePtr.inl: In member function ‘void sf::ResourcePtr< <template-parameter-1-1> >::OnResourceDestroyed()’:
/usr/local/include/SFML/System/ResourcePtr.inl:148:18: error: ‘NULL’ was not declared in this scope

在文件 ResourcePtr.inl 中使用“NULL”似乎有问题。就个人而言,我通常会避免使用这个关键字,而是使用 0,但是当它用于我自己甚至没有编写的包文件中时,我该怎么办?此外,如果没有我的管理员权限,我将无法编辑该文件以更正它,但我使用 sudo 编辑它并添加了#include &lt;cstddef&gt;。这为一系列类似问题打开了闸门,其中“未定义”或关键字或类型无法识别(列表太长,我无法在此处发布)。这似乎表明库中缺少某些内容。最初设置库时我遇到了很多麻烦,所以我意识到我可能没有正确完成。您可以在this question here 中看到它。有谁知道我做错了什么和/或我可以做些什么来解决这个问题?

【问题讨论】:

    标签: c++ linux compiler-errors sfml


    【解决方案1】:

    此代码无法编译并显示您描述的错误:

    #include <SFML/Graphics/Image.hpp>
    int main()
    {
        return 0;
    }
    

    输出是:

    In file included from ./include/SFML/System/Resource.hpp:211:0,
                     from ./include/SFML/Graphics/Image.hpp:31,
                     from test.cpp:1:
    ./include/SFML/System/ResourcePtr.inl: In constructor ‘sf::ResourcePtr< <template-parameter-1-1> >::ResourcePtr()’:
    ./include/SFML/System/ResourcePtr.inl:31:12: error: ‘NULL’ was not declared in this scope
    ./include/SFML/System/ResourcePtr.inl: In member function ‘void sf::ResourcePtr< <template-parameter-1-1> >::OnResourceDestroyed()’:
    ./include/SFML/System/ResourcePtr.inl:148:18: error: ‘NULL’ was not declared in this scope
    

    幸运的是,修复很简单,这很有效:

    #include <SFML/System.hpp>
    #include <SFML/Graphics/Image.hpp>
    int main()
    {
        return 0;
    }
    

    看起来这也有效:

    #include <cstddef>
    #include <SFML/Graphics/Image.hpp>
    int main()
    {
        return 0;
    }
    

    在使用NULL 之前,他们忘记在代码中的某处包含此标头。只要你在他们的任何标题之前包含它,它就可以工作。

    正如您自己所说,NULL 的使用可能有点令人困惑,因为它本身就是一个扩展为 0(void*)0 的宏。新标准通过引入强类型的nullptr 来解决这个问题。在 ResourcePtr.inl 的第 31 和 148 行将 NULL 更改为 nullptr 可能是解决问题的最佳方法。

    【讨论】:

    • 值得注意的是(void*)0是C中NULL的常用定义,但在C++中却不能这样。在 C++98 中,它必须是值为 0 的整数常量,例如 00L
    猜你喜欢
    • 2010-10-02
    • 2014-12-08
    • 1970-01-01
    • 1970-01-01
    • 2011-10-08
    • 2011-01-31
    • 1970-01-01
    • 2021-05-13
    相关资源
    最近更新 更多