By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,998 Members | 2,815 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,998 IT Pros & Developers. It's quick & easy.

multiple inheritance

P: 5
Hi all,

I got a c++ compiler error: cannot allocate an object of abstract type.
The thing is:
I defined a class slave_cpu like the following:

class slave_cpu :
public sc_module,
public virtual basic_slave_base < ADDRESS_TYPE , REG_TYPE >,
public interrupt_receiver< INDEX_TYPE >
{
public:
explicit slave_cpu( sc_module_name module_name, int n = 3, unsigned int device_id = 1, sc_time cycle = sc_time(2,SC_NS), unsigned short n_bank = 4, sc_time m_delay = sc_time(8,SC_NS) );
....
};

for the base class basic_slave_base and interrupt_receiver

template< class ADDRESS , class DATA >
class basic_slave_base :
public virtual basic_if< ADDRESS , DATA > ,
public virtual tlm_transport_if<
basic_request< ADDRESS , DATA > ,
basic_response< DATA > >
{
...
}

template< class INDEX >
class interrupt_receiver :
public virtual interrupt_if<INDEX> ,
public virtual tlm_transport_if<
interrupt_request<INDEX>, interrupt_response >
{
...
}

both basic_slave_base and interrupt_receiver have a virtual base class tlm_transport_if, but with different template parameters.
tlm_transport_if is defined as

template < typename REQ , typename RSP >
class tlm_transport_if : public virtual sc_interface
{
public:
virtual RSP transport( const REQ & ) = 0;

};

the problem is that when I instantiate a object of slave_cpu as
slave_cpu *slave = new slave_cpu(sc_module_name(sc_gen_unique_name("slave _cpu")), CPU_REG, i+1, CLOCK_CYCLE, n_bank, mem_delay);

I got a compiler error : cannot allocate an object of abstract type 'mpsim::slave_cpu'.
This instantiation used to be fine, before I made slave_cpu derived from interrupt_receiver< INDEX_TYPE >. Only after I added interrupt_receiver as one of slave_cpu base class, I got this error.
I guess it is due to the multiple inheritance. But I can not find where causes this problem. Can you please give me any help?

Thanks in advance
Aug 10 '07 #1
Share this Question
Share on Google+
4 Replies


weaknessforcats
Expert Mod 5K+
P: 9,197
Class tlm_transport_if has a pure virtual function. That means you cannot call that function with a tlm_transport_if object. That, in turn, neabs tghe compile won't let you create a tlm_transport_if object because you are not trustworth and might go ahead and call that pure virtual virtual function.

You can derive from tlm_transport_if but you must implement RSP transport() in the derived class.

Does slave_cpu implement this function??
Aug 10 '07 #2

P: 5
Thank you very much for your reply.

I implement RSP transport() in both interrupt_receiver and basic_slave_base which are intermediate class between tlm_transport_if and slave_cpu. Actually, I need two kind of implementations for transport() for different kind of purpose.

template< class INDEX >
class interrupt_receiver :
public virtual interrupt_if<INDEX> ,
public virtual tlm_transport_if<
interrupt_request<INDEX>, interrupt_response >
{
using interrupt_if<INDEX>::isr_job;
using interrupt_if<INDEX>::isr_timer;
using interrupt_if<INDEX>::isr_resume;


public:
typedef tlm_transport_if< interrupt_request<INDEX>, interrupt_response > if_type;

interrupt_response transport( const interrupt_request<INDEX> &request ) {

interrupt_response response;

switch( request.type ) {
case NEW_JOB :
response.status = isr_job( request.index );
break;
case TIMER:
//response.status = this->write( request.a , request.d );
response.status = isr_timer( request.index );
break;
case RESUME:
//response.status = this->write( request.a , request.d );
response.status = isr_resume( request.index );
break;
default :
response.status = ERROR;
break;

}

return response;

}


};

template< class ADDRESS , class DATA >
class basic_slave_base :
public virtual basic_if< ADDRESS , DATA > ,
public virtual tlm_transport_if<
basic_request< ADDRESS , DATA > ,
basic_response< DATA > >
{
using basic_if<ADDRESS , DATA >::write;
using basic_if<ADDRESS , DATA >::read;
//using basic_if<ADDRESS , DATA >::read_nodelay;
public:
typedef tlm_transport_if<
basic_request< ADDRESS , DATA > ,
basic_response< DATA >
> if_type;

basic_response<DATA> transport( const basic_request<ADDRESS,DATA> &request ) {

basic_response<DATA> response;

switch( request.type ) {
case READ :
response.status = read( request.a , response.d );
break;
case WRITE:
//response.status = this->write( request.a , request.d );
response.status = write( request.a , request.d );
break;

default :
response.status = ERROR;
break;

}

return response;

}


};

Is that ok? because I think although both basic_slave_base and interrupt_receiver derive from tlm_transport_if, but having different template parameter, I am wondering whether compiler will generate an object for each tlm_transport_if case. thanks again


Class tlm_transport_if has a pure virtual function. That means you cannot call that function with a tlm_transport_if object. That, in turn, neabs tghe compile won't let you create a tlm_transport_if object because you are not trustworth and might go ahead and call that pure virtual virtual function.

You can derive from tlm_transport_if but you must implement RSP transport() in the derived class.

Does slave_cpu implement this function??
Aug 10 '07 #3

weaknessforcats
Expert Mod 5K+
P: 9,197
I am wondering whether compiler will generate an object for each tlm_transport_if case. thanks again
Yes it will unless you use virtual base classes. If you do that, you will fall under new constructor rules and will have tio specify which base class constructors should be called for a particulare derived class. You will need to specify bot the direct and the indirect base classes.
Aug 11 '07 #4

P: 5
thanks, I found the error. It was caused by some virtual functions in somewhere else which were not implemented at all. Anyway, thank you so much
Aug 13 '07 #5

Post your reply

Sign in to post your reply or Sign up for a free account.