2013-09-28 37 views
0

如果我有具有同一类中定义的两个结构体可以互相访问吗?

struct A{ 
    B *ptr; //it says identifier undefined 
}; 
struct B{ 

}; 

两者都在相同的类定义的两个结构的一类的书籍。如上所述,是否可以将struct B的指针保存在struct A中? 任何人都可以帮助PLZ?

+2

认沽'结构B''结构A'。 – us2012

+0

非常感谢=)为我工作 – sara

+4

[很多](http://hyperboleandahalf.blogspot.co.uk/2010/04/alot-is-better-than-you-at-everything.html)。 – jrok

回答

2

你只需要声明struct B第一:

struct B; 
struct A{ 
    B *ptr; //it says identifier undefined 
}; 
struct B{ 

}; 
3

在C++中,所有符号必须在使用前声明。因此,只需将B结构放置在A结构之前即可。

1

struct B; 

第一,你就大功告成了。这告诉编译器将会有一个B

1

是的,你可以,你只需要declare the structure name before你使用实例的结构。

struct B; // declared before struct A, now the problem is gone. 

struct A{ 
    B *ptr; //it says identifier undefined 
}; 


struct B{ 

}; 
1

地方支柱乙结构A.前

class book 
{ 
struct B 
{ 

}; 
struct A 
{ 
B * ptr;   
}; 

}; 

地方结构乙befor结构A.前

相关问题