Set_type_override_by_type

I am getting an error while running the following code on EDA Playground. Can anyone help me resolve. Thanks.

`include "pkg.sv"
import pkg::*;

module top();
   
  write_xtn w_h;
  
  function void build();
    w_h=write_xtn::type_id::create("w_h");
    w_h.randomize;
    w_h.print();
  endfunction
  
  initial 
  begin    
    
    for(int i=0;i<5;i=i+1)
    build();
        
	factory.set_type_override_by_type (write_xtn::get_type(),short_xtn::get_type());
    //set_type_override_by_type(write_xtn::get_type(),short_xtn::get_type());
    
    for(int i=0;i<5;i=i+1) 
    begin
    build();
    end
    
  end
  
endmodule

Error Details
Error-[XMRE] Cross-module reference resolution error
testbench.sv, 20
Error found while trying to resolve cross-module reference.
token ‘factory’. Originating module ‘top’.
Source info: factory.set_type_override_by_type(write_xtn::get_type(),
short_xtn::get_type());

In reply to Abijith Prakash:

Is this your code? There are a few weaknesses:
(1) Why to include the file pkg.sv?
(2) There is no object of the factory (see your override command)

In reply to Abijith Prakash:

You can either use
uvm_factory factory = uvm_coreservice_t::get().get_factory();
factory.set_type_override_by_type (write_xtn::get_type(),short_xtn::get_type());

OR
set_type_override(write_xtn::get_type(),short_xtn::get_type());

In reply to chr_sue:

this was the code from my training school. I have just tried to redo it on EDA playground using synopsys VCS simulator.

pkg.sv includes the other files of TB. I have just included the top module above.

In reply to Abijith Prakash:

OK, but the module does not know the factory. You have the declare this.
And what is in the package pkg you are importing?

In reply to chr_sue:

package pkg;

import uvm_pkg::*;
`include “uvm_macros.svh”

include "tb_defs.sv" include “write_xtn.sv”
`include “short_xtn.sv”

endpackage

In reply to chr_sue:

I am including and importing the package because i was getting an error in EDA playground.

In reply to Abijith Prakash:

It looks like you are including and importing the same. Right?

In reply to chr_sue:

Yes.

In reply to chr_sue:

I declared the handle factory and used factory = uvm_coreservice_t::get().get_factory();. It worked. Thanks.

In reply to amansoor:

Thanks for the response.

I declared the handle factory and used factory = uvm_coreservice_t::get().get_factory();. It worked. Thanks.