我正在尝试编译下面的程序。然而,它给了我error: unknown type name 'constexpr'错误。我该怎么办?
代码:
//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;
}在使用constexpr的每一行中,我都得到了相同的错误。
error: unknown type name 'constexpr'
constexpr int intervals = 96;我的C/C++配置.json文件如下:
{
"configurations": [
{
"name": "Mac",
"includePath": [
"${workspaceFolder}/**"
],
"defines": [],
"macFrameworkPath": [],
"compilerPath": "/usr/local/bin/gcc-11",
"cStandard": "gnu17",
"cppStandard": "gnu++17",
"intelliSenseMode": "macos-gcc-x64"
}
],
"version": 4
}系统: MACOSX
IDE: VSCode
用g++在Mac终端上编译
编译命令:g++ model.cpp battery.cpp load.cpp
发布于 2021-08-05 09:46:04
如果直接从命令行调用编译器,则不使用该json配置。在这种情况下,您必须自己指定每个选项:
g++ -std=gnu++17 -Wall -Werror model.cpp battery.cpp load.cpp我只是添加了-Wall -Werror,你不应该在没有它们的情况下编译代码。
如果没有-std选项,编译器将使用没有constexpr的较早版本的C++标准。
https://stackoverflow.com/questions/68664151
复制相似问题