程式碼:
int *p = nullptr; // 或 int *p = 0; / NULL
// 編譯錯誤:'nullptr' was not declared in this scope
nullptr。-std=c++11 (建議 -std=c++17)。#include <iostream>
int main() {
int *p = nullptr; // C++11
if (!p) std::cout << "空指標\n";
int *q = 0; // C++98 相容
int *r = NULL; // 需 #include
return 0;
}
-Wall -Wextra -pedantic -std=c++17-g;Release:加 -O2 -s。-lstdc++fs (GCC < 9)。SetConsoleOutputCP(65001) (Windows)。CMakeLists.txt,建議改用 CMake + VS Code / CLion 更現代。