Is it posible to write implementation of non template method in template class(struct) at .cpp file? I have read that template method should be written on .h, but my method is not template method although it belongs to template class. Here is the code in my .h:
#include <iostream>
#ifndef KEY_VALUE_H
#define KEY_VALUE_H
using namespace std;
namespace types
{
template <class T, class U>
struct key_value
{
T key;
U value;
static key_value<T, U> make(T key, U value)
{
key_value<T, U> kv;
kv.key = key;
kv.value = value;
return kv;
};
string serialize()
{
// Code to serialize here I want to write in .cpp but fails.
}
};
}
#endif /* KEY_VALUE_H */
I tried to write the implementation of method serialize()
in .cpp file like this:
#include "key_value.h"
using namespace types;
template <class T, class U>
string key_value<T, U>::serialize()
{
// Code here returning string
}
Ended up with error: Redefinition of 'serialize'
How is the proper way to doing this?