tags:

views:

102

answers:

2

If I have a template definition like the one below, can someone provide a code sample for how I would actually instantiate an instance of this with two of my own classes?

template <class T1, class T2>
class LookUpTable { 
public:
    LookUpTable(); 
    void set(T1 x, T2* y);
    T2* get(T1 x);
};

Thanks.

+5  A: 

You can't instantiate it unless you provide a definition for the constructor. And you won't be able to use it unless you provide definitions for the other two functions. If you did provide them, you would instantiate it something like:

LookUpTable <std::string, int> t;

or if you have your own classes A and B:

LookUpTable <A, B> t;

It looks like this is a map of some sort, in which case you may as well use std::map:

#include <map>
#include <string>

std::map <std::string, int> m; 
anon
That's what I thought.. the thing I wasn't sure about was the parameterless constructor definition (and the class name) not containing any type parameters, I come from a C# background. Presumably the constructor implementation would look like `LookUpTable() { *initialise internals* }`?
MalcomTucker
@Malcolm Yes, although you may also want to investigate constructor initialisation lists. And as I suggested, if this is not a learning exercise, use std::map.
anon
It is a learning exercise, and thanks for helping out :)
MalcomTucker
+2  A: 

1) Since this is a template class, make sure your constructor and functions are declared in the header.

2) Instantiate it like this:

LookUpTable <YourClass1, YourClass2> table;

3) Note: you have a typo, Tl instead of T1. In some fonts l looks almost like 1.

Igor Krivokon