我从这里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
#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
错误消息g ++ 11:
error: 'constants::pi' declared as an 'inline' variable
答案 0 :(得分:4)
从C ++ 17开始允许使用内联变量。
您需要在页面上指定-std=c++17
选项
编译器命令行。