I am trying to create an interface that is a pure abstract class. But the static functions I get "unresolved external symbol", unless I specifically define the data type in the .cpp, for example, an "int". Here is my code:
Box.h (public header) `
#pragma once
#include <list>
template< typename T>
class IBox {
public:
virtual int ShowThings(std::list<T>& things) = 0;
virtual int Fill(const T &thing) = 0;
virtual int Remove(const T &thing) = 0;
static IBox<T> *Create();
static void Destroy(IBox<T> *box);
};
`
Box_.h (private header) `
#include "Box.h"
template<class T>
class Box : public IBox<T> {
private:
std::list<T> things;
public:
int ShowThings(std::list<T>& things);
int Fill(const T &thing);
int Remove(const T &thing);
};
`
Box.cpp `
#include "Box_.h"
template<class T>
int Box<T>::ShowThings(std::list<T>& things) {
things = this->things;
return 0;
}
template<class T>
int Box<T>::Fill(const T &thing) {
this->things.push_back(thing);
return 0;
}
template<class T>
int Box<T>::Remove(const T &thing) {
this->things.remove(thing);
return 0;
}
template<class T>
IBox<T> *IBox<T>::Create(){
return new Box<T>();
}
template<class T>
void IBox<T>::Destroy(IBox<T> *box) {
delete box;
box = nullptr;
}
`
main.cpp `
#include "Box.h"
#include <iostream>
int main()
{
IBox<int> *box = IBox<int>::Create(); // ERROR: unresolved external symbol (LNK2019)
return 0;
}
`
The problem is solved if in Box.cpp, I specifically define functions for "int". For example:
`
IBox<int> *IBox<int>::Create() {
return new Box<int>();
}
void IBox<int>::Destroy(IBox<int> *box) {
delete box;
box = nullptr;
}
`
I would like to know if this can be done without specifically defining it. Thanks!