未声明函数中使用的结构成员
Undeclare structure member used in function?
我的函数定义不会编译,因为有一个所谓的"未声明"结构成员。上面写着"错误:‘gen’未申报"。但我声明它,甚至在之后初始化它(为了简单起见,我省略了那个部分)。你能明白为什么这个不能编译吗?
我的结构:
typedef struct Parent {
int gen; //I DECLARE IT RIGHT HERE!!!
char *name;
struct Parent * next;
child * child;
} parent;
和我的函数:
void findP(parent* x){ //function findP
headP = p;
tempC = headP->child;
int check = 0;
while(headP != NULL){
while(tempC != NULL){
if (x->name == tempC->name){
x->gen = gen--; //HERES WHERE I USE IT
findP(headP); //recursion here
check++;
break;
}
if(check > 0) break;
tempC = tempC->next;
}
if(check > 0) break;
headP = headP->next;
}
}
x->gen = gen--;
CCD_ 2中的CCD_ 1当然是未声明的。
我认为它应该是指x->gen--;
x->gen = gen--;
x->gen
存在。gen
?右边的东西是什么?你是指以下内容吗?
x->gen--;
您已声明gen
,但已将其声明为structParent的成员。顺便说一句,在结构声明前面不需要typedef
。
这个问题不明显的原因是您陷入了一个常见的陷阱,即不能使结构/类成员变量不同。许多开发团队采用了一种最佳实践,即用前导m_
、m
或尾随gen
0来区分成员变量名。m_
似乎是最主要的方式。
struct Parent {
int m_gen; //I DECLARE IT RIGHT HERE!!!
char * m_name;
Parent * m_next;
child * m_child;
};
typedef Parent parent; // if you're hell-bent on having both versions.
现在问题应该变得非常明显:
while(headP != NULL){
while(tempC != NULL){
if (x->m_name == tempC->m_name){
x->m_gen = gen--; //HERES WHERE I USE IT
gen
不是某个事物的成员,它是一个局部变量。如果添加m_前缀
x->m_gen = m_gen--; //HERES WHERE I USE IT
很明显,你还没有对右边的m_gen
说"member of what",这不是一个成员函数。
相关文章:
- 具有结构成员char数组的sscanf
- 仅使用结构名称访问结构成员
- 使用 structType*& 赋给结构成员
- 使用结构成员指针在C++中填充结构
- 聚合初始化,将成员指针设置为同一结构成员
- 为什么结构化绑定不使用"auto&"返回对结构成员的引用,而是返回成员本身
- 是否可以将结构数组别名为结构成员数组?
- 结构成员在访问时被清除
- 如何指示结构成员没有锯齿?
- 为什么继承的结构成员在联合中无法访问?
- 尝试在 qml 中访问结构成员的 QList
- C++/CLI -- 访问结构成员
- 调用在类中声明的结构成员
- 对结构成员的临时绑定引用
- 无法使用 strcpy 将字符串复制到静态结构成员
- 为什么类成员数据必须是静态的才能被模板化类的模板化结构成员访问
- 填充模板化结构类型的结构成员
- C/C++intellisense 0.26.1不显示结构成员列表
- 在源文件中创建具有结构成员的结构
- 如何访问结构成员作为指针