[C++] error: NULL' was not declared in this scope がでた

NULLって常に使えるのかと思っていました。
何もincludeしないクラス作成して、その中でNULL使ったら以下のメッセージが出て怒られました。

error: NULL' was not declared in this scope

ググってみるとstackoverflow先生の記事が見つかりました。

#include <cstddef>

インクルードすればOKでした。
ちなみに上記のファイルをみてみると、

:
#undef __need_size_t
#undef __need_NULL
#undef __need_wint_t
#include <bits/c++config.h>
#include <stddef.h>
:

となっていて、実際にはstddef.hでNULLが提議されていました。
その内容を探してみると...
/usr/lib/gcc/x86_64-linux-gnu/7/include/stddef.h

:
#undef NULL  /* in case <stdio.h> has defined it. */
#define NULL __null
#define NULL ((void *)0)
#define NULL 0
:

と定義されていました。

0 件のコメント :

コメントを投稿