1

I am trying to compile the below program. However it's giving me the error: unknown type name 'constexpr' error. What should I do?

Code:

//this is model.cpp. battery.cpp and load.cpp are two secondary files
#include "load.h"
#include "battery.h"

//compile time constants - Settings
constexpr int intervals = 96;
constexpr float e_min = 0.001;
constexpr int max_iters = 100;
constexpr int nloads = 10;
constexpr int nbat = 10;


struct devices
{
    load loadDevices[nloads];
    battery batteryDevices[nbat];
};

int main()
{


    //Initialization of the model
    devices devices1;

    return 0;
}

I get the same error on each line where constexpr is used.

error: unknown type name 'constexpr'
constexpr int intervals = 96;

My C/C++ configurations .json file is as follows:

{
    "configurations": [
        {
            "name": "Mac",
            "includePath": [
                "${workspaceFolder}/**"
            ],
            "defines": [],
            "macFrameworkPath": [],
            "compilerPath": "/usr/local/bin/gcc-11",
            "cStandard": "gnu17",
            "cppStandard": "gnu++17",
            "intelliSenseMode": "macos-gcc-x64"
        }
    ],
    "version": 4
}

System: MACOSX

IDE: VSCode

Compiling on Mac's Terminal using g++

compilation command: g++ model.cpp battery.cpp load.cpp

4
  • 3
    -std=c++11/14/17/20 ? Commented Aug 5, 2021 at 9:39
  • Hi Jarod,I am a beginner programmer in C++. Are you saying I should type g++ -std=c++11 model.cpp battery.cpp load.cpp in the mac terminal to compile? Commented Aug 5, 2021 at 9:42
  • 1
    Yes (even use more recent standard if possible). Giving version of gcc might help else. Commented Aug 5, 2021 at 9:44
  • Okay. The constexpr error goes away when I compile with -std=c++11 and onwards. However now post compiling I get the Undefined symbols for architecture x86_64: error for the method "optAlg::bufferPlanning(float*, float, float, int, float*, float*, float, float, float*, float*, bool, float*, int, float*, float*)", referenced from: battery::plan(float*) in battery-011c04.o Commented Aug 5, 2021 at 9:50

1 Answer 1

1

That json config is not used if you are calling the compiler directly from the command line. In that case you have to specify every option yourself:

g++ -std=gnu++17 -Wall -Werror model.cpp battery.cpp load.cpp

I just added -Wall -Werror for good measure, you should never compile your code without them.

Without the -std option the compiler uses an older version of the C++ standard that does not have constexpr.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.