Я пытаюсь сделать пример использования шаблонов, чтобы узнать, как их использовать. Я сделал проект в Visual Studio 2010 C++ с тремя файлами:Почему этот код шаблона шаблона не компилируется?
controller.cpp:
#include <iostream>
#include "Foo.h"
using namespace std;
int main(){
Example<int,string> example;
example.appols(5, "Hi");
return 0;
}
foo.h:
#include <iostream>
using namespace std;
template <class T, class E>
class Foo{
public:
void printThis(T t, E e);
};
template <class T, class E>
class Example{
public:
void appols(T t, E e);
};
Foo.ipp:
#include <iostream>
#include "Foo.h"
using namespace std;
template<class T, class E>
void Foo<T, E>::printThis(T t, E e){
cout << "FOO! " << t << ' ' << e << endl;
}
template<class T, class E>
void Example<T, E>::appols(T t, E e){
cout << "APPOLS!! " << t << ' ' << e << endl;
Foo<T,E> f;
f.printThis(t,e);
}
Когда я пытаюсь скомпилировать, я получаю:
1>------ Build started: Project: Template Practive, Configuration: Debug Win32 ------
1>Controller.obj : error LNK2019: unresolved external symbol "public: void __thiscall Example<int,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > >::appols(int,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >)" ([email protected][email protected][email protected][email protected]@[email protected]@[email protected]@[email protected]@[email protected]@@@[email protected][email protected]@[email protected]@[email protected]@[email protected]@[email protected]@@Z) referenced in function _main
1>C:\Users\Matthew\documents\visual studio 2010\Projects\Template Practive\Debug\Template Practive.exe : fatal error LNK1120: 1 unresolved externals
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
Не могли бы вы помочь мне увидеть, что я сделал неправильно здесь и откуда error LNK2019
? Спасибо заранее!
После определения класса вам нужно включить 'Foo.ipp' в' Foo.h'. – juanchopanza