mardi 14 juillet 2020

Separating template class definition from declaration - explicit instantiation

I have a .h and .cpp file

Foo.h

template<typename T>
class Foo {
public:
  void g();
  void h();
};

Foo.cpp

#include <iostream>
#include "Foo.h"
template<typename T>
void Foo<T>::g()
{
  std::cout << "Foo<T>::g()\n";
}
template<typename T>
void Foo<T>::h()
{
  std::cout << "Foo<T>::h()\n";
}

The above two files are in abc/def directory and I have the implementation in a different cpp file

Foo-impl.cpp

#include <../../Foo.cpp>
template class Foo<int>;

This Foo-impl.cpp is in abc/ghi/jkl directory.

But when I use #include <abc/def/Foo.cpp I get an error saying that the file doesn't exist. But when I use #include <abc/def/Foo.h>, I don't get file doesn't present erro (the file is identified by the compiler) but I get linker error.

I am using the implementation as per link

Why does it work with <../../Foo.cpp> and why not with <abc/def/Foo.cpp. How should I make it work ?

Aucun commentaire:

Enregistrer un commentaire