Illegal location for a hierarchical name (in a package).Issue

Dear All,

I’m trying to resolve the “illegal location for a hierarchical name (in a package)” issue.

`include "sub.sv"
`include "package.sv"

module testbench;
  sub u_sub();
   
  initial
    begin
      import my_pkg::*;
      cadd tr;
      tr=new();
      tr.add();
    end
endmodule 
      

class cadd;
   
  int a;
  
  task add();
    a=u_sub.b; 
    $display("the value of a is %0d",a);
  endtask
  
endclass 
 
module sub;
  int b;
  initial
    begin
      b=7;
    end
endmodule


package my_pkg;

`include "add.sv"
endpackage

this make a error within 
 a=u_sub.b;

Quite short but bunch files , so I just represent the link instead code at here.

https://www.edaplayground.com/x/2rN4

And I want to know How to avoid this implementation and how to resolve this problem.

In reply to UVM_LOVE:

Packages need to be coded such that there are no other dependancies on other code, other than importing another package. You cannot refer to module instances from within a package.

You need to declare your
class cadd
inside the testbench, or you need to use more elaborate methods as desribed in my DVCon paper.