Ada 《美国残疾人法案》;“类型没有约束”;实例化记录时出错
我有一些Ada代码,如下所示:Ada 《美国残疾人法案》;“类型没有约束”;实例化记录时出错,ada,Ada,我有一些Ada代码,如下所示: type NODE; type NODE_PTR is access NODE; type PRINT_PTR is access procedure(X: NODE_PTR); type NODE is record parent : NODE_PTR; left : NODE_PTR; right : NODE_PTR; id : INTEGER;
type NODE;
type NODE_PTR is access NODE;
type PRINT_PTR is access procedure(X: NODE_PTR);
type NODE is
record
parent : NODE_PTR;
left : NODE_PTR;
right : NODE_PTR;
id : INTEGER;
visit : PRINT_PTR;
end record;
procedure PRINT(X : NODE_PTR) is
...
end PRINT;
root : NODE_PTR;
id_value : INTEGER;
...
root := new NODE(NULL, NULL, NULL, id_value, PRINT'access);
...
root := new NODE'(NULL, NULL, NULL, id_value, PRINT);
稍后在我的代码中,我尝试实例化如下节点:
type NODE;
type NODE_PTR is access NODE;
type PRINT_PTR is access procedure(X: NODE_PTR);
type NODE is
record
parent : NODE_PTR;
left : NODE_PTR;
right : NODE_PTR;
id : INTEGER;
visit : PRINT_PTR;
end record;
procedure PRINT(X : NODE_PTR) is
...
end PRINT;
root : NODE_PTR;
id_value : INTEGER;
...
root := new NODE(NULL, NULL, NULL, id_value, PRINT'access);
...
root := new NODE'(NULL, NULL, NULL, id_value, PRINT);
这是:
ret_ptr : NODE_PTR;
id_value : INTEGER;
...
ret_ptr := new NODE(parent, NULL, NULL, id_value, PRINT'access);
当试图“gnatmake”这段代码时,编译器会抱怨两个assign语句:“invalid constraint:type has no discriminant”。
发生了什么?据我所知,我没有在节点中定义任何判别式,也没有尝试在这些新的节点调用中分配任何判别式。当使用初始值分配访问类型时,您需要一个“如下所示:
type NODE;
type NODE_PTR is access NODE;
type PRINT_PTR is access procedure(X: NODE_PTR);
type NODE is
record
parent : NODE_PTR;
left : NODE_PTR;
right : NODE_PTR;
id : INTEGER;
visit : PRINT_PTR;
end record;
procedure PRINT(X : NODE_PTR) is
...
end PRINT;
root : NODE_PTR;
id_value : INTEGER;
...
root := new NODE(NULL, NULL, NULL, id_value, PRINT'access);
...
root := new NODE'(NULL, NULL, NULL, id_value, PRINT);
当分配具有初始值的访问类型时,您需要一个如下所示的'
type NODE;
type NODE_PTR is access NODE;
type PRINT_PTR is access procedure(X: NODE_PTR);
type NODE is
record
parent : NODE_PTR;
left : NODE_PTR;
right : NODE_PTR;
id : INTEGER;
visit : PRINT_PTR;
end record;
procedure PRINT(X : NODE_PTR) is
...
end PRINT;
root : NODE_PTR;
id_value : INTEGER;
...
root := new NODE(NULL, NULL, NULL, id_value, PRINT'access);
...
root := new NODE'(NULL, NULL, NULL, id_value, PRINT);
正确的。对于
“
,节点”(…)
是一个限定表达式。如果没有它,NODE(…)
是一个子类型——或者如果NODE
具有适当的判别式,则是这样。对。对于“
,节点”(…)
是一个限定表达式。如果没有它,NODE(…)
是一个子类型——或者如果NODE
具有适当的判别式,它就是一个子类型。