Шаблонный класс и *.cpp
есть файл test.h
в нем определен интерфейс класса
#ifndef TestH #define TestH template<class Type> class Test { public: void Proc(); }; #endif
Есть файл test.cpp:
в нем определена реализация
#include "Test.h" template<class Type> void Test<Type>::Proc() { ... };
И файл main.cpp:
в котором это все используется
#include "test.h" void main() { Test<int> mTest; mTest.Proc( ); };
При компиляции выдается ошибка "error LNK2019: unresolved external symbol "public: void __thiscall Test<int>::Proc(void)" (?Proc@?$Test@H@@QAEXXZ) referenced in function _main"
Как решить эту проблему?
Ответ:
На данный момент эту проблему решить можно только одним способом - писать реализацию методов также в заголовочном файле, поскольку ни один из современных компиляторов не поддерживает template export (кроме Comeau начиная с версии 4.3.01)
MSDN о Visual Studio:
The export keyword is not supported on templates.
Справка Intel C++:
The Intel® C++ Compiler conforms to the ANSI/ISO standard (ISO/IEC 14882:1998) for the C++ language, however, the export keyword for templates is not implemented. For compatibility with Microsoft Visual C++*, the compiler also supports many extensions to the C++ language. For more information, see the Microsoft Visual C++ documentation.
Добавление: поддержка export появилась в более новых версиях Intel C++(9.0+).
Обсуждение на форуме - http://www.gamedev.ru/forum/?group=0&topic=25838
29 сентября 2005 (Обновление: 16 июня 2006)