我有以下 CORBA IDL 3.2,它试图声明一个相互递归的结构:
module SE
{
interface SE
{
typedef unsigned short MenuItemID; // a small integer representing a unique menu item
enum MenuSubaction { CollectCharacter, CollectStruct };
struct MenuItemAction; // forward declaration
union MenuSubactionParameter switch (MenuSubaction)
{ case CollectStruct: MenuItemAction sub_structure; // <<<<<<<<< use of forward
};
struct MenuItemAction { MenuSubaction menu_subaction;
MenuSubactionParameter menu_subaction_parameter;
};
}; // interface
}; // module
我在标有 <<<<<
的行上收到来自 Sun JDK 1.7 idlj 的投诉 ... SE.idl (line xx): Illegal reference to incomplete forward declaration of type MenuItemAction.
注意:这不是“前向接口(interface)”声明。
什么是“不完整的前向声明”? (如果您成功声明为前向声明,我不会认为前向声明不完整,只是尚未定义。也许这只是一个容易误解的短语)。
更重要的是,我如何设法定义我的递归结构?
我是 CORBA 的新手,所以我真的不知道 :-} 我在做什么。我不明白为什么 CORBA 不能定义这样的递归结构;一个传输不会递归的特定实例。特别是,这形成了一棵树,对于 CORBA 来说应该是“容易”发送的。
编辑:Brian 的答案是正确的。我需要替换前向引用的直接提及,
MenuItemAction sub_structure
与
sequence<MenuItemAction> sub_structure>
最佳答案
您可以转发声明结构,但有很多限制。
编辑:我不知道您使用的是哪个版本的 CORBA,但是在 2.6.1 specification 中它在第 3.10.2.3 节(强调我的)中说:
The IDL syntax allows the generation of recursive structures and unions via members that have a sequence type.
之后:
IDL supports recursive types via a forward declaration for structures and unions (as well as for valuetypes).
之后:
An incomplete type can only appear as the element type of a sequence definition. A sequence with incomplete element type is termed an incomplete sequence type.
An incomplete sequence type can appear only as the element type of another sequence, or as the member type of a structure or union definition.
例子:
struct Foo; // Forward declaration; Foo is incomplete
typedef sequence<Foo> FooSeq; // incomplete sequence type
struct Foo {
long value;
FooSeq chain; // incomplete seq. type used as struct member; OK
};
可以在链接中找到更多信息,包括这个示例,它可能更接近您想要执行的操作:
union Bar; // Forward declaration
typedef sequence<Bar> BarSeq;
union Bar switch(long) { // Define incomplete union
case 0:
long l_mem;
case 1:
struct Foo {
double d_mem;
BarSeq nested; // OK, recurse on enclosing
// incomplete type
} s_mem;
};
关于corba - 如何在 CORBA IDL 中声明/使用对结构的前向引用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17256682/