有人可以告诉我为什么会收到此错误消息吗?内联 constexpr 变量
Can someone tell me why I get this error message? inline constexpr variable
我按照这里的教程进行操作 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
从 C++17 开始允许使用内联变量。
您需要指定 -std=c++17
选项
编译器命令行。
我按照这里的教程进行操作 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
从 C++17 开始允许使用内联变量。
您需要指定 -std=c++17
选项
编译器命令行。