sfml编译错误:此范围内未声明NULL

时间:2013-03-17 04:30:22

标签: c++ linux compiler-errors sfml

我正在运行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 <cstddef>。这使得闸门打开了类似问题的行列,其中“未定义”或关键字或类型未被识别(列表对我来说太长了,无法在此处发布)。这似乎表明图书馆遗漏了一些东西。我最初设置库时遇到了一些麻烦,所以我意识到我可能没有正确完成它。您可以在this question here中看到它。有谁知道我做错了什么和/或我能做些什么来解决这个问题?

1 个答案:

答案 0 :(得分:2)

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

#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可能是解决问题的最佳方法。