Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sfml compile error: NULL was not declared in this scope

I am running Linux Mint. The version information is as follows:

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

I am attempting to compile and link files for a C++ program that uses sfml. When attempting to compile the files, I get the following error:

$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

It seems to have a problem with the use of "NULL" in the file ResourcePtr.inl. Personally, I would normally refrain from using this keyword and instead use 0, but what can I do when it's used in a package file that I myself didn't even write? Moreover, without my administrator privileges, I would not be able to edit the file to correct it, but I used sudo to edit it and added #include <cstddef>. This opened the floodgates to a cavalcade of similar problems where something "wasn't defined" or a keyword or type was unrecognized (the list is far too long for me to post here). This seems to indicate that something is missing from the library. I had quite a bit of trouble setting up the library initially, so I realize I may not have done it correctly. You can see it in this question here. Does anyone know what I'm doing wrong and/or what I can do to fix this issue?

like image 933
rurouniwallace Avatar asked Feb 14 '26 01:02

rurouniwallace


1 Answers

This code does not compile and shows the errors you described:

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

The output is:

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

Luckily the fix is easy, this works:

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

It looks like this works too:

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

They forgot to include this header somewhere in their code before using NULL. As long as you include it before any of their headers it should work.

As you said yourself, the use of NULL can be a bit confusing, as it is itself a macro expanding either to 0 or (void*)0. The new standard takes care of that by introducing nullptr, which is strongly typed. Changing NULL for nullptr in lines 31 and 148 of ResourcePtr.inl is probably the best way to solve the problem.

like image 60
Thibaut Avatar answered Feb 16 '26 15:02

Thibaut