【发布时间】:2021-07-26 08:17:26
【问题描述】:
我正在尝试使用 OOP 概念在 System Verilog 中执行二叉树插入和顺序遍历。在创建对象之前,我收到错误消息正在使用该对象。请看一下代码,如果有人发现任何错误,请帮助我
class node;
byte data;
node left;
node right;
function new();
this.data = data;
this.left = null;
this.right = null;
endfunction
endclass
class bin_search extends node;
node newNode;
node nd,root,current,parent;
byte in_data;
function new();
super.new();
this.in_data = in_data;
endfunction
function automatic insert(in_data);
newNode.data = nd.data;
if(root.data == null) begin
root = newNode;
return;
end
else begin
current = root;
parent = null;
end
forever begin
parent = current;
if(in_data < current.data) begin
current = current.left;
if(current.left == null) begin
parent.left = newNode;
return;
end
end
else begin
current = current.right;
if(current.right == null) begin
parent.right = newNode;
return;
end
end
end
endfunction
function automatic inorder_traverse(node node_tr);
//using nodes here
endfunction
endclass
module binary;
node NODE;
bin_search bs;
byte ins;
initial begin
NODE = new;
bs = new;
bs.insert(50);
$display("Binary search tree after insertion:");
bs.inorder_traverse(bs.root);
end
endmodule
错误信息: 错误 - [NOA] 空对象访问 二进制.sv, 28 解引用深度 1 的对象在使用之前被使用 构造/分配。 请确保对象在使用前已分配完毕。
【问题讨论】:
-
您的代码没有显示您抱怨的插入机制。该消息告诉您未能检查递归路径中某处的“null”节点引用(未在您的示例中显示)。
-
顺便说一句,没有必要在你的类方法中添加
automatic关键字;他们总是有自动的生命周期。 -
嗨@Serge,谢谢你的回复。我在问题中编辑了我的函数插入。我找不到我做错的地方。请查看已编辑的问题。
标签: class oop system-verilog uvm