c++ - 这个编译器错误是什么意思 - C++中的“'='标记之前的声明中的限定ID”?
问题描述
我试图了解类中私有 const 的用法。我的理解是 private const 用于在类中使某些东西保持不变,而 static 则用于拥有一个副本。
最初,我的代码使用的是 pi,它的数据类型是浮点数。因此,我尝试将 float 更改为 int,因为我读到 const static 仅允许用于类中的 int 类型。
#include <iostream>
class MyExample
{
private:
static const int x;
};
int main(void)
{
int const MyExample::x = 3;
std::cout<<"x value is "<<MyExample::x<<std::endl;
return 0;
}
编译——
$g++ -std=c++14 test.cpp
test.cpp: In function ‘int main()’:
test.cpp:12:27: error: qualified-id in declaration before ‘=’ token
int const MyExample::x = 3;
我知道移动“int const MyExample::x = 3;”这一行 从 main() 到外部,如果我也删除私有,则删除错误。
解决方案
我有同样的问题,但是当我把静态成员初始化放在主函数之外时,问题就解决了,像这样:
int const MyExample::x = 3;
int main(){...}
推荐阅读
- session - 无法覆盖 Koa/next.js 中的会话
- ruby-on-rails - Rails 6 部署作为开发影响生产
- amazon-web-services - A2Hosting 用于电子邮件的共享主机计划,DNS 和网站托管在 AWS 上
- ms-media-foundation - 从 IMFMediaType 获取编解码器名称
- facebook-graph-api - 如何获取在 Facebook 群组中发表评论的用户的姓名?
- python-3.x - DjangoRestAPI 过滤列表字段
- node.js - 根据类的字符串名称创建类的实例
- python - 为 matplotlib.pyplot.plot() 生成的每个标记设置连接样式
- c# - 当我在 .NET 5 中运行应用程序时,Serilog 没有创建日志 SQL 表
- java - Spring Boot 中的部分更新查询抛出“BsonInvalidOperationException”