//includes, using etc.
int main()
{
List<int> a;
cout << a.size() << endl;
return 0;
}
//list.h
template <class T>
class List{
int items;
public:
List();
~List();
int size() const;
};
//list.cpp
#include "list.h"
template<class T>
List<T>::List() :
items(0)
{}
template<class T>
List<T>::~List()
{}
template<class T>
int List<T>::size() const
{ return items; }
Это должно работать, не так ли? Когда я определяю содержимое list.h и list.cpp над основной функцией, все работает нормально. Тем не менее, это дает мне некоторые ошибки:C++ Templates - undefined reference
main.cpp:(.text+0x12): undefined reference to
List<int>::List()'
List::size() const'
main.cpp:(.text+0x1e): undefined reference to
main.cpp:(.text+0x4f): undefined reference toList<int>::~List()'
List::~List()'
main.cpp:(.text+0x64): undefined reference to
при изменении List<int> a;
в основной функции List<int> a();
только ошибки я получаю это:
main.cpp:10:12: error: request for member ‘size’ in ‘a’, which is of non-class type ‘List()’
Помоги мне, что случилось?
См. Http://stackoverflow.com/questions/495021/why-can-templates-only-be-implemented-in-the-header-file – Jarod42
Необходимо указать определение и реализацию шаблона в одном файле. – andre