Pencilcaseman
Pencilcaseman

Reputation: 400

Code compiles in CLion but not in Command Prompt

I am trying to a C++ library and need (want) to use nested namespaces in order to increase readability of the code. However, I am running into a problem when trying to compile my code in the Windows Command Prompt with g++ main.c.

The code below is an example of what I will have -- a nested namespace and then some functions or classes:

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

The code below is an example for my main.c file:

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

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

    return 0;
}

When I run this simple probram in CLion, it compiles and runs perfectly, however when I run the code in the Windows 10 Command Prompt, I get the following error telling me something about the namespaces:

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
 }
 ^

I was wondering what I am doing wrong and how I can fix this problem.

Thanks!

Upvotes: 0

Views: 491

Answers (1)

brc-dd
brc-dd

Reputation: 13004

Try updating your g++ version to 6.1.0 or higher.

The code doesn't compiles on g++ v5.5.0 even with -std=gnu++17 flag. You can check that here. (Reason: Nested namespaces weren't supported by the compiler then.)

The code shall compile with compiler defaults (without any flags) on g++ v6.1.0 or higher. You can check this here.

You can check your compiler version by running: g++ --version on cmd.

Pro Tip: Locate your CLion's compiler and if it is g++ then add that one to path. (No need of wasting internet data on updating the old g++ compiler!)

Upvotes: 2

Related Questions