0

I have some template struct defined in templates.h:

//templates.h
#ifndef TEMPLATES_H
#define TEMPLATES_H

template<typename Key = int, typename Value = Key> 
struct array{ 
     map<const Key, Value> data;

     //Stuffs 
};

/*explicit (full) specialization*/

template<>
template<typename Key>
struct array<Key, char>{

    map<const Key, Value> data;

    //Stuffs
};

#include "templates.cpp" 
#endif

Trying to separate declarations (interface) from definitions (implementations), i did this in templates.cpp:

//templates.cpp
#ifdef TEMPLATES_H
#include "templates.h"
#endif

template<typename Key, char> 
array<Key, char>::array(const Key& k, const char* v){ cout << "specialised array<T, char>"; } 

inside main(), i test-drove:

//inside main();
array<int, char> out(1, "Just testing");      /*error 1: prototype for 'array<Key, char>::array(const Key&, const char*)' does not match any in class 'array<Key, char>'
                                                error 2: candidate is: array<Key, char>::array(const array<Key, char>&)    
                                                */

Question: I expected a call to array::array(Key, char) constructor; which is an explicit specialization of array::array(Key, Value), but got the error message commented above. what am i doing wrong, or how can i fix this ?.

Osagie Odigie
  • 281
  • 1
  • 3
  • 13
  • 1
    You have to put template definition in the header file. Template classes won't work if you separate the definition part into a cpp file. – xuhdev Jul 05 '16 at 19:11
  • @xuhdev The `.cpp` file is `#include`d in the `.h` file. – Waleed Khan Jul 05 '16 at 19:19
  • Why do you include "templates.h" again in templates.cpp? Also, do you compile only your main function file, or you compile templates.cpp as well? – xuhdev Jul 06 '16 at 00:30

0 Answers0