1

I have template class

template<class T1 ,class T2, class T3>
class server_fd
{
  T1 servport;
  T2 server_ip;
  T3 servobj;
  int local_port;

public:
  server_fd(const T1& servport, const T2& server_ip,const T3 servobj) {
    this->servport=servport;
    this->server_ip=server_ip;
    this->servobj=servobj;   

    cout <<"check \n"<<this->server_ip;
  }
};    

Now i want to store the object of this class in map ,so i am declaring map like this..

 map<int,server_fd* > MapPairList;

After this when i complile my code it give this error error: template argument 2 is invalid error: template argument 4 is invalid

However if i dont use template class i am able to define map successfully.

Is there any other way to define map for storing template class object ? Please let me know.

MSalters
  • 173,980
  • 10
  • 155
  • 350
user1693566
  • 13
  • 1
  • 4
  • How about `map*>` with your custom types? Keep in mind, that templates are no generics at all and every item in the map has to have the same combination of three types! Therefore you cannot mix `server_fd` with `server_fd` in one map unless you define some interfaces and encapsulate the functions and accessors there. – Christian Ivicevic Sep 24 '12 at 09:19
  • 1
    `server_fd` is not a type, it's a template. You need to give it specific types for T1,T2,T3 to use it. – Flexo Sep 24 '12 at 09:19
  • I have tried this map*>==> map*> not sure if the third arguement i have put correctly..as in T3 i have to passed server object. I am still getting error Transportlayer.cpp:44: error: type/value mismatch at argument 3 in template parameter list for ‘template class server_fd’ Transportlayer.cpp:44: error: expected a type, got ‘server_fd’ Transportlayer.cpp:44: error: template argument 2 is invalid Transportlayer.cpp:44: error: template argument 4 is invalid – user1693566 Sep 24 '12 at 11:01

2 Answers2

0

If you can abstract somehow the access to those types, you can have your template class inherit from an abstract base class, and than just keep pointers to the base in your map.

It's called type erasure, and you can get more info about it here Type erasure techniques

Community
  • 1
  • 1
StoryTeller - Unslander Monica
  • 165,132
  • 21
  • 377
  • 458
  • I have tried using this map*> map*> – user1693566 Sep 24 '12 at 11:00
  • @user1693566: Your third argument `T3` to the template `server_fd` must also be a type. You're passing `server_fd` there too. That's probably wrong - why pass `server_fd` to `server_fd` ? - and certainly wrong since `server_fd` is a template and therefore not a match for the type paramter `T3`. – MSalters Sep 24 '12 at 11:30
  • In T3 i want to store generic object ..let say i have TCPserver and UDPServer class and i want to store the object of these class in map ,so if i declare map like this map*> – user1693566 Sep 24 '12 at 11:49
  • So In T3 i want to store generic object ..let say i have TCPserver and UDPServer class and i want to store the object of these class in map ,so if i declare map like this map*> this will not solve my purpose .. as for UDP server i have to decelar again then like this right map*>..Actually i want to develop client server appllication and store the object to TCP and UDP server on map ,so when i poll on fds i can retrive apporopriate object and from there on i can call connect or recvfr – user1693566 Sep 24 '12 at 11:55
  • So i want to declare global generic map to do this which can store tcp and udp class objects – user1693566 Sep 24 '12 at 11:56
0

Try adding a base class to your template class:

class server_fd_entry {};

template<class T1 ,class T2, class T3>
class server_fd
{
    <snip>
}

...

map<int,server_fd_entry* > MapPairList;

This way the map can store all server_fd types regardless of template parameters. I can see this leading to a lot of downcasting, however, so the alternative is to ensure you specify all the template parameters when you declare the map:

map<int,server_fd<int,int,int>* >

This way you can only store one specific type of server_fd, but if that's all you need it will probably be neater.

Bok McDonagh
  • 1,367
  • 10
  • 27