Re: What's wrong with this template definition?
Bertwim wrote:
#include <map>
template <class T>
class symtblpp
{
private:
typedef std::map< std::string, T > maptype;
Note: the element type of the map is
std::pair<std::string const, T>
here!
In the implemantation file (.cpp) the member function lookup is defined:
Be sure to read the FAQ, I see some problems here explained there.
template <typename T>
T* symtblpp<T>::lookup( const std::string& key )
{
maptype::iterator it = _map.find( key );
return &(*it);
}
'*it' returns above-mentioned element type and from that you take the
address. This will not yield a 'T*' obviously. Also, you failed to check
whether the key was found. If you actually know it will be found, I would
suggest returning a reference instead, so that you document that it will
never return null. Even if it might be missing, it could be a good idea to
do so and throw an exception if it is not found instead.
symtblpp.cpp:42: error: expected `;' before ???it???
symtblpp.cpp:45: error: ???it??? was not declared in this scope
where line 42 refers to the line with the iterator.
Can somebody explain to me what's wrong?
[...]
{ Short answer: use "typename" in front of maptype::iterator. -mod/sk }
This is correct but not enough to make this code run. It could also be any
number of other silly mistakes. If the above and the lecture of the FAQ
don't help, I would suggest you create a simple example consisting of just
a single file.
Uli
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]