当文件中使用模板时的多文件编译问题
我使用了三个文件template.cpp,myhead.h,myhead.cpp;
内容如下:
//template.cpp
#include <iostream>
#include "myhead.h"
using namespace std;
int main(int argc, char *argv[])
{ int a,b;
a=1,b=2;
cout<<Max(a,b)<<endl;
}
//myhead.h
#ifndef myhead_h
#define myhead_h
template<typename T>
T Max(T ,T ) ;
#endif
//myhead.cpp
#include "myhead.h"
template <typename T>
T Max(T a,T b)
{return (a>b?a:b);}
用borland c++5.5 命令行编译时(bcc32 template myhead)出错如下:
Microsoft Windows XP [版本 5.1.2600]
(C) 版权所有 1985-2001 Microsoft Corp.
E:\BCC55\source>bcc32 template myhead
Borland C++ 5.5.1 for Win32 Copyright (c) 1993, 2000 Borland
template.cpp:
Warning W8057 template.cpp 11: Parameter 'argc' is never used in function main(i
nt,char * *)
Warning W8057 template.cpp 11: Parameter 'argv' is never used in function main(i
nt,char * *)
myhead.cpp:
Turbo Incremental Link 5.00 Copyright (c) 1997, 2000 Borland
Error: Unresolved external 'int Max<int>(int, int)' referenced from E:\BCC55\SOU
RCE\TEMPLATE.OBJ
当把T换成int时编译可以通过,请问这是为什么,谢谢