-1

i try to make simple template class but in the dtor it gives compilation error: this is what i have :

#include <map>

template<class k , class v> 
class ObjectMap
{
    public:
        ObjectMap(k key, v value)
        {
            InnerObjectMap = new std::map<key, value>();
        }
        ~ObjectMap();
    private:
        std::map<k,v> *InnerObjectMap;
};

and here is the cpp file that has only the dtor

#include "ObjectMap.h"
ObjectMap::~ObjectMap()
{

}

put im getting the compilation error :

1>  ObjectMap.cpp
1>\objectmap.cpp(6): error C2955: 'ObjectMap' : use of class template requires template argument list
1>          \objectmap.h(10) : see declaration of 'ObjectMap'
1>          \objectmap.h(10) : see declaration of 'ObjectMap'
1>\objectmap.cpp(7): error C2509: '{dtor}' : member function not declared in 'ObjectMap'
1>          \objectmap.h(10) : see declaration of 'ObjectMap'
1>\objectmap.cpp(7): fatal error C1903: unable to recover from previous error(s); stopping compilation

what im doing wrong here ?

user63898
  • 29,839
  • 85
  • 272
  • 514

2 Answers2

1

Your ObjectMap is not just class. It's a class template.

It should be:

#include <map>

template <class k, class v> 
class ObjectMap
{
    public:
        ObjectMap(k key, v value)
        {
            InnerObjectMap = new std::map<key, value>();
        }
        ~ObjectMap();
    private:
        std::map<k,v> *InnerObjectMap;
};

template <class k, class v>
ObjectMap<k, v>::~ObjectMap()
{
    // do stuff
}

Notice that the destructor is also in the class template definition file, not *.cpp file. Otherwise, the compiler won't be able to instantiate your destructor.

ikh
  • 10,119
  • 1
  • 31
  • 70
  • why is that ? do you say i can't set the destructor in its own cpp file ? – user63898 Jun 08 '15 at 10:44
  • @user63898 Yes. When the compiler instantiate your destructor in another module, it doesn't know the destructor - it only knows `~ObjectMap();`, which is in the header file. – ikh Jun 08 '15 at 10:47
  • 1
    @user63898 this will help you in detail . http://stackoverflow.com/questions/495021/why-can-templates-only-be-implemented-in-the-header-file – Steephen Jun 08 '15 at 10:48
  • @Steephen Thanks. That's what I really want to link. – ikh Jun 08 '15 at 10:49
  • Thanks , but what if i have in this class also member functions that are not related to the template where should i implement them ? – user63898 Jun 08 '15 at 11:16
  • @user63898 `not related to the template` the `ObjectMap` is a template. Its member *is* related to template. – ikh Jun 08 '15 at 11:20
1
InnerObjectMap = new std::map<key, value>();

Above line of code should rewrite as follows, since std::map declaration expects types instead values;

InnerObjectMap = new std::map<k, v>();

Your destructor definition should change as follows since it is a template:

template<class k , class v> 
ObjectMap<k,v>::~ObjectMap()
{

}

You can see more details on why you should implement templates in header file from this SO link : Why can templates only be implemented in the header file?

Community
  • 1
  • 1
Steephen
  • 14,645
  • 7
  • 40
  • 47