[英]Can someone tell me why I get this error message? inline constexpr variable
I follow a tutorial from here https://www.learncpp.com/cpp-tutorial/global-constants-and-inline-variables/我从这里按照教程https://www.learncpp.com/cpp-tutorial/global-constants-and-inline-variables/
main.cpp主文件
#include <iostream>
#include "constants.h"
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
int main() {
std::cout << "Enter a radius: ";
int radius{};
std::cin >> radius;
std::cout << "The circumference is: " << 2 * radius * constants::pi;
return 0;
}
constants.h常量.h
#ifndef CONSTANTS_H
#define CONSTANTS_H
// define your own namespace to hold constants
namespace constants
{
inline constexpr double pi { 3.14159 }; // note: now inline constexpr
inline constexpr double avogadro { 6.0221413e23 };
inline constexpr double my_gravity { 9.2 }; // m/s^2 -- gravity is light on this planet
// ... other related constants
}
#endif
error message g++11:错误消息 g++11:
error: 'constants::pi' declared as an 'inline' variable
Inline variables are allowed starting from C++17.从 C++17 开始允许内联变量。
You need to specify the -std=c++17
option on the compiler command line.您需要在编译器命令行上指定
-std=c++17
选项。
( https://en.cppreference.com/w/cpp/language/inline ) ( https://en.cppreference.com/w/cpp/language/inline )
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.