我定义了一系列函数
func.h中
声明了一个模板函数
template <class T> 
void show_array(const T ar[], int const n);func.cpp中定义了它;但在主文件中
#include "fun.h"main(..)
{
  int a[] = ...;
  double b[] = ..;
  show_array(a,10);
  show_array(b,10);
}报错:
: error LNK2001: unresolved external symbol "void __cdecl show_array(double const * const,int)" (?show_array@@YAXQBNH@Z): error LNK2001: unresolved external symbol "void __cdecl show_array(int const * const,int)" (?show_array@@YAXQBHH@Z)Debug : fatal error LNK1120: 2 unresolved externals--------------------------------------------------------
直接定义在主文件内却没有错,为什么?

解决方案 »

  1.   

    main.cpp里
    #include "fun.cpp"模板不支持编译分离
      

  2.   

    回复人: yzkzero(Skall You) ( ) 信誉:100  2005-03-19 17:02:00  得分: 0  
     
     
       main.cpp里
    #include "fun.cpp"------------------------------------------------我照你这么做了,
    但是其它定义的函数又报错:
    unc.obj : error LNK2005: "char * __cdecl itobs(int,char *)" (?itobs@@YAPADHPAD@Z) already defined in CPrimer.objfunc.obj : error LNK2005: "int __cdecl invert_end(int,int,char *)" (?invert_end@@YAHHHPAD@Z) already defined in CPrimer.objfunc.obj : error LNK2005: "char * __cdecl end2front(char *,int)" (?end2front@@YAPADPADH@Z) already defined in CPrimer.objDebug/CPrimer.exe : fatal error LNK1169: one or more multiply defined symbols found
    Error executing link.exe.CPrimer.exe - 4 error(s), 0 warning(s)