简体   繁体   English

有人可以告诉我为什么会收到此错误消息吗? 内联 constexpr 变量

[英]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.

相关问题 有人可以告诉我为什么即使参数正确也出现此错误? - Can someone tell me why am i getting this error even though the arguments are correct? 有人能告诉我为什么这不是一个持续的表达? - Can someone tell me why this is not a constant expression? 有人能告诉我为什么会收到这个错误吗? - can someone tell why am i getting this error? 我试图用这段代码找到二叉树的高度,但它一直返回 0,有人能告诉我为什么吗? - I am trying to find the height of a Binary tree with this code, but it keeps returning 0, can someone tell me why? 有人可以告诉我为什么输入“ y”继续后仍停留在验证循环中吗? - Can someone tell me why I am stuck in my validation loop after entering 'y' to continue? 有人可以告诉我为什么此代码不打印任何内容吗? - Can someone please tell me why this code doesnt print anything? 有人可以告诉我为什么我的分数总是计算为零? - Can someone tell me why my fraction always calculates as zero? 我看到“使用未声明的标识符”错误,有人能告诉我如何解决吗? - I'm seeing "use of undeclared identifier" error, can someone tell me how to fix it? 为什么我不能使用constexpr全局变量来初始化constexpr引用类型? - Why can I not use a constexpr global variable to initialize a constexpr reference type? 有人可以告诉我为什么传递参数后我的字符串为空吗? - Can someone tell me why my string is blank after passing the argument?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM