If the default argument is initialized in the base_class function there is no error. But for the following explicit super.new the following error is obtained. Thanks in Advance

Error-[SV-SNCIM] Super new call is missing 5soln.sv, 13 $unit Base class constructor specification is missing in derived class ‘sub_class’. Please add ::new arguments to ‘extends’ specification or add call to ‘super.new’
class base_class;
bit[15:0] data;
function new(bit[15:0] data/=6/);
this.data=data;
endfunction
function display ();
$display (“[Base] data=%0h”, data);
endfunction
endclass

class sub_class extends base_class;
bit[3:0] id;
bit[2:0] mode;
function new2(bit [3:0] id, bit[2:0] mode=2);
super.new(6);
this.id=id;
this.mode=mode;
endfunction
function display();
super.display();
$display (“[Child] id=%h,mode=%h”,id,mode);
endfunction
endclass
module tb;
initial
begin
sub_class sc1=new();
sc1.new2(22,3);
sc1.display();
end
endmodule

In reply to chashy:

You need to show the prototype code for the base and extended class constructors.

Hi Sir,
In the code if the base class is declared with the value 6 there is no error,but when I try to assign value using super.new(6) then the error is seen.
Is it because of the base’s new is always called, but the “data” argument is NOT provided for the IMPLICIT call to new in base_class.
Even then 1)Add default argument in constructor of base class.
—function new(bit[15:0] data=6)—
2)Explicitly call super.new with required arguments.
------super.new(6);-----
Both should have worked but I see only first option is working for the above code.So should I follow only first option??

In reply to chashy:

It’s difficult to make-out anything from data you shared. can you show the full code ?

Example code:

class C;
  int c3;
  function new(int a = 5);
    c3 = a;
  endfunction
endclass

class D extends C;
  int d1;
  function new;
    super.new(10);
    d1 =4;
  endfunction
endclass

module my_module;

  D d;
 
  initial begin
    d = new();
  end
endmodule

  • If you want to create an explicit user-defined new method (even in extended class), you must use the “function new()” not “function new2()”

  • As you have a custom user defined new method() in base class, you must declare the explicit user-defined new method in extended class. (Just change the new2() to new(), it should work)

In reply to yourcheers:

Subclass sc1=new(); is declared earlier itself. As suggested I had executed using new also it resulted in error.

In reply to chashy:

Every class have new constructor function. If a class does not provide an explicit user-defined new method, an implicit new method shall be provided automatically. The new method of a derived class shall first call its base class constructor super.new().

Let take your sub_class example.


class sub_class extends base_class;
bit[3:0] id;
bit[2:0] mode;
function new2(bit [3:0] id, bit[2:0] mode=2);
super.new(6);
this.id=id;
this.mode=mode;
endfunction
function display();
super.display();
$display ("[Child] id=%h,mode=%h",id,mode);
endfunction
endclass

It doesn’t have explicit user-defined new class constructor method. new2 method define by you isn’t constructor. so here implicit new method shall be provided automatically which have code something like this.

function new();
    super.new();
endfunction

But your base class need super.new(6) instead of super.new() if you don’t have default value to argument data in base new method. so it was giving error when you didn’t define default value.

function new(bit[15:0] data/*=6*/);
this.data=data;
endfunction

so if you don’t define default value to argument in base new method and don’t have explicit call to super.new with argument value then you will get error.

Let fix your example:


class base_class;
bit[15:0] data;
  
function new(bit[15:0] data/*=6*/);
this.data=data;
endfunction
  
function display ();
$display ("[Base] data=%0h", data);
endfunction
endclass

class sub_class extends base_class;
  
bit[3:0] id;
bit[2:0] mode;

//changed new2 to new, Now class have explicit new method  
function new(bit [3:0] id, bit[2:0] mode=2);
super.new(6);  
this.id=id;
this.mode=mode;
endfunction
  
function display();
super.display();
$display ("[Child] id=%h,mode=%h",id,mode);
endfunction
endclass

module tb;
initial
begin
sub_class sc1=new(22,3);
//sc1.new2(22,3);
sc1.display();
end
endmodule

In reply to Rahulkumar Patel:

Thank you.