【问题标题】:g++ 'nullptr' was not declared in this scopeg++ 'nullptr' 未在此范围内声明
【发布时间】:2012-07-24 23:45:37
【问题描述】:

我在带有 git-bash 的 Windows 8 Release Preview 上使用 gcc-4.7.1。

$ g++ -v
Using built-in specs.
COLLECT_GCC=c:\Users\nikhil bhardwaj\mingw64\bin\g++.exe
COLLECT_LTO_WRAPPER=c:/users/nikhil bhardwaj/mingw64/bin/../libexec/gcc/x86_64-w
64-mingw32/4.7.1/lto-wrapper.exe
Target: x86_64-w64-mingw32
Configured with: /home/drangon/work/mingw-w64-dgn/source/gcc/configure --host=x8
6_64-w64-mingw32 --target=x86_64-w64-mingw32 --disable-nls --enable-languages=c,
c++,objc,obj-c++ --with-gmp=/home/drangon/work/mingw-w64-dgn/build/for_target --
enable-twoprocess --disable-libstdcxx-pch --disable-win32-registry --prefix=/hom
e/drangon/work/mingw-w64-dgn/target --with-sysroot=/home/drangon/work/mingw-w64-
dgn/target
Thread model: win32
gcc version 4.7.1 20120524 (prerelease) (GCC)

当我尝试编译一段小代码sn-p时,

using namespace std;
struct node
{
    int data;
    node *left, *right;
};
node *newNode(int data)
{
    node *node = new (struct node);
    node->data = data;
    node->left = nullptr;
    node->right = NULL;
    return node;
}

我收到此错误,

$ g++ -I../includes bst.cpp
bst.cpp: In function 'node* newNode(int)':
bst.cpp:13:18: error: 'nullptr' was not declared in this sc
bst.cpp:14:19: error: 'NULL' was not declared in this scope

我无法使用NULLnullptr,我需要包含一些头文件吗?

【问题讨论】:

  • NULL 在许多 C-legacy 标头中定义(<cstddef> 可能是 C++ 中的首选标头)。它不是自动定义的关键字。

标签: gcc null g++


【解决方案1】:

在c++11模式下试试:

g++ -std=c++11 -I../includes bst.cpp

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多