如何访问在另一个结构中定义的结构的成员?
假设我们有一个结构定义为:
struct FR {
size_t n;
struct FR_1{
unsigned char r;
unsigned char g;
unsigned char b;
};
};
在2015年视觉工作室下,写作:
struct FR x;
x.FR_1.
不显示FR_1成员的选项。另一方面,写作:
struct FR_1 y;
说:Error,incomplete type is not allowed.
你如何处理这种结构?
答案 0 :(得分:8)
示例声明类型struct FR_1
,不是该类型的成员。相反,尝试:
struct FR {
size_t n;
struct FR_1 {
unsigned char r;
unsigned char g;
unsigned char b;
} fr1;
};
FR x;
x.fr1.r = 0;
答案 1 :(得分:4)
struct FR {
size_t n; // < Declaration of member variable
struct FR_1{ // < Declaration of nested type
unsigned char r;
unsigned char g;
unsigned char b;
};
FR_1 fr1; // < Declaration of member variable
};
您需要在FR_1
structrure中声明FR
类型的变量,而不仅仅是类型本身。
FR fr;
fr.fr1.r = 0;
答案 2 :(得分:0)
您需要实际创建结构的实例。正常的struct
声明遵循表格
struct struct-name {
members
} inst;
所以你需要将其声明为
struct FR {
size_t n;
struct FR_1 {
unsigned char r;
unsigned char g;
unsigned char b;
} fr1;
};
现在你可以写
了FR fr;
fr.fr1.r = 255;
. . .