2012-07-23 239 views
4

我在git-bash的windows 8 Release Preview上使用gcc-4.7.1。g ++'nullptr'未在此範圍內聲明

$ 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) 

當我嘗試編譯一個小的代碼片段,

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,我做需要包含一些頭文件?

+2

NULL在許多的C-遺留報頭的定義(''可能是在C中的優選的標題++)。它不是自動定義的關鍵字。 – 2012-07-23 16:37:11

回答

7

嘗試在C++ 11模式:

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

任何想法如何讓vim不將該行標記爲錯誤? – nikhil 2012-07-23 16:29:17

+0

我沒有看到這種行爲,您是否使用插件? – perreal 2012-07-23 16:35:01

+0

我使用這個https://github.com/akitaonrails/vimfiles來設置vim。 – nikhil 2012-07-23 16:37:22