代码在 CLion 中编译,但在命令提示符中不编译

Code compiles in CLion but not in Command Prompt

我正在尝试使用 C++ 库并且需要(想要)使用嵌套名称空间以提高代码的可读性。但是,当我尝试使用 g++ main.c.

在 Windows 命令提示符中编译我的代码时,我 运行 遇到了一个问题

下面的代码是我将拥有的示例——一个嵌套的命名空间,然后是一些函数或 类:

namespace gpc::warning {
    void raiseError() {
        std::cout << "Error...\n";
        exit(1);
    }
}

下面的代码是我的 main.c 文件的示例:

#include <iostream>
#include "Warning/raise.hpp"

int main() {
    std::cout << "Hello, World!" << std::endl;
    gpc::warning::raiseError();

    return 0;
}

当我在 CLion 中 运行 这个简单的程序时,它编译并且 运行 完美,但是当我 运行 Windows 10 命令提示符中的代码时,我收到以下错误,告诉我有关命名空间的一些信息:

In file included from main.cpp:2:0:
Warning/raise.hpp:10:14: error: expected '{' before '::' token
 namespace gpc::warning {
              ^
Warning/raise.hpp:10:16: error: 'warning' in namespace '::' does not name a type
 namespace gpc::warning {
                ^
main.cpp: In function 'int gpc::main()':
main.cpp:9:10: error: 'gpc::warning' has not been declared
     gpc::warning::raiseError();
          ^
main.cpp: At global scope:
main.cpp:12:1: error: expected '}' at end of input
 }
 ^

我想知道我做错了什么以及如何解决这个问题。

谢谢!

尝试将您的 g++ 版本更新到 6.1.0 或更高版本。

即使使用 -std=gnu++17 标志,代码也不会在 g++ v5.5.0 上编译。您可以检查 here。 (原因:当时的 编译器 不支持嵌套命名空间。)

代码应在 g++ v6.1.0 或更高版本上使用编译器默认值(无任何标志)进行编译。你可以检查这个 here.

您可以通过 运行 检查您的编译器版本:g++ --version on cmd

专业提示:找到您的 CLion 编译器,如果它是 g++,则将其添加到路径中。 (无需浪费互联网数据来更新旧的 g++ 编译器!)