问题描述
我正在尝试使用 OOP 概念在 System Verilog 中执行二叉树插入和顺序遍历.在创建对象之前,我收到了正在使用该对象的错误消息.请看一下代码,如果有人发现任何错误,请帮助我
I am trying to perform binary tree insertion and in order traversal in System Verilog using OOPs concepts. I am getting error that the object is being used before creating it. Please take a look at the code and help me if someone find any mistakes
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 的对象在使用之前被使用构建/分配.使用前请确保对象已分配.
Error message:Error-[NOA] Null object accessbinary.sv, 28The object at dereference depth 1 is being used before it wasconstructed/allocated.Please make sure that the object is allocated before using it.
推荐答案
从下一行抛出空错误.
newNode.data = nd.data;
在 bin_searchnewNode
和 nd
对象是通过调用 new()
构建的/code> 类或 insert()
方法本身.您需要先创建类的对象,然后才能访问它们的成员或方法.
I don't see neither newNode
nor nd
objects being constructed by calling new()
either in constructor of bin_search
class or in the insert()
method itself.You need to create objects of classes before accessing their members or methods.
这篇关于系统 verilog 中的空对象访问错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!