Problem with overloaded virtual function

A

A. Saksena

Hi all,
I am trying to compile following code :-
=======================================
#include <iostream.h>
#include <string>
namespace tlm
{
template <typename REQUEST, typename RESPONSE >
class tlm_transport_if
{
public:
// Virtual function: for implementation of transport layer
virtual RESPONSE transport (const REQUEST &) = 0;
}; // end of class tlm_transport_if

}

class master_out_if
{
private:
class inherit: public tlm::tlm_transport_if<int,bool >,
public tlm::tlm_transport_if<char,bool >,
public tlm::tlm_transport_if<double,bool >
{};
public:
typedef inherit type;
};


class x:public master_out_if::type
{
public:
bool transport (const int&){}
bool transport (const char &){}
bool transport (const double &){}

};



int main()//this can be bus model

{
bool var;
master_out_if::type *var1=new x;
var1->transport((int)(100));

}
==========================================


The problem is that I get a compile time error which says

-----------------------------------------------------------
test2.cpp: In function `int main()':
test2.cpp:55: cannot allocate an object of type `x'
test2.cpp:55: because the following virtual functions are abstract:
test2.cpp:13: RESPONSE tlm::tlm_transport_if<REQUEST,
RESPONSE>::transport(const REQUEST&) [with REQUEST = double, RESPONSE =
bool]
test2.cpp:56: request for member `transport' is ambiguous
test2.cpp:13: candidates are: RESPONSE tlm::tlm_transport_if<REQUEST,
RESPONSE>::transport(const REQUEST&) [with REQUEST = double, RESPONSE =
bool]
test2.cpp:13: RESPONSE tlm::tlm_transport_if<REQUEST,
RESPONSE>::transport(const REQUEST&) [with REQUEST = char, RESPONSE =
bool]
test2.cpp:13: RESPONSE tlm::tlm_transport_if<REQUEST,
RESPONSE>::transport(const REQUEST&) [with REQUEST = int, RESPONSE =
bool]
--------------------------------------------------------------------


Does that mean overloading of virtual functions is not allowed?. If not how
I can implement this functionality


Abhishek
 
A

Alf P. Steinbach

* A. Saksena:
Hi all,
I am trying to compile following code :-
=======================================
#include <iostream.h>

ERROR. Don't use <iostream.h>.

It's is a pre-standard header that's not part of standard C++.

Use standard

#include <iostream>

BAD STYLE. Don't include headers that you don't use.

#include <string>

BAD STYLE. Don't include headers that you don't use.

namespace tlm
{
template <typename REQUEST, typename RESPONSE >

BAD STYLE. Don't use all uppercase for non-macro names.

class tlm_transport_if

BAD STYLE. Don't use the namespace name as prefix for things declared
in the namespace -- the namespace provides a good enough prefix.

BAD STYLE. Don't use inconsistent indentation.
{
public:
// Virtual function: for implementation of transport layer
virtual RESPONSE transport (const REQUEST &) = 0;
}; // end of class tlm_transport_if

}
class master_out_if
{
private:
class inherit: public tlm::tlm_transport_if<int,bool >,
public tlm::tlm_transport_if<char,bool >,
public tlm::tlm_transport_if<double,bool >
{};
public:
typedef inherit type;

BAD STYLE. Don't use silly roundabout ways. If you mean the nested
class to be public, declare it public in the first place.
};


class x:public master_out_if::type
{
public:
bool transport (const int&){}

ERROR. The function must return a value.

bool transport (const char &){}
bool transport (const double &){}

};



int main()//this can be bus model

{
bool var;

BAD STYLE. Don't declare variables that you don't use.

master_out_if::type *var1=new x;
var1->transport((int)(100));

}
==========================================


The problem is that I get a compile time error [...]

Does that mean overloading of virtual functions is not allowed?.

It means that when you have same-named identifiers in different
subobjects (here base class subobjects), the name look up fails:
function signatures are not considered at that point.

If not how I can implement this functionality

Add a 'using' for each function in class 'master_out_if::type'.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,227
Latest member
Daniella65

Latest Threads

Top