'class MyClass' 在尝试定义静态结构实例时不会命名类型
‘class MyClass’ does not name a type when trying to define a static struct instance
我正在尝试定义/声明一个静态结构实例,但是当我使用g++ -std=c++11 -g -Wall -c test.cc
编译时,我不断收到错误test.cc:3:1: error: ‘mine’ in ‘class MyClass’ does not name a type
这是我的代码的样子:
//test.h
#ifndef TEST_H
#define TEST_H
#include <stdint.h>
struct my_struct {
uint8_t foo;
uint8_t bar;
};
class MyClass {
public:
static const struct my_struct mine;
};
#endif
//test.cc
#include "test.h"
MyClass::mine = {1, 2};
在 C 中,你需要使用 struct StructName
来声明一个对象。
但在C++,在宣布之前struct
和class
都是不必要的。
相关文章:
- 如何检查派生类的类型?(C++实例)
- 对于多态类型T,如何在没有类型T实例的情况下获得指向T的虚拟表的指针
- 为什么找不到使用命名空间中定义的类型实例化的 std::weak_ptr 的重载运算符==?
- 关于QLIST类型实例复制构造函数的一个奇怪问题
- 使用非量表类型实例化模板时修复编译错误
- 如何根据参数的类型实例化不同的类
- 如何使用引用类型实例化模板函数
- 防止为字符串流提取运算符不支持的类型实例化模板类 (>>)
- 编译器是否也将所有参数依赖类型实例化,即使超载函数的参数计数不匹配
- 访问模板类型实例成员
- 使用合格类型实例化的模板内部功能的名称分辨率
- 从C (例如Main.CPP)访问注册类型实例
- 强制static_assert在类型实例化期间激发
- 在运行时根据不同的类型实例化模板类
- 按类型实例化C++ lambda
- 如何在 C++11 中将 vector 与引用类型实例对象一起使用
- 标准容器模板可以用不完整类型实例化吗?
- visual c++泛型类型实例化
- 基于运行时类型实例化构造函数
- 如何用自己的类型实例化超类模板