头文件声明如下:
template<class T> class combination{
public:
……
bool have_next_combination();
void get_next_combination(output_vector &)const;
T* operator[](const unsigned&); public:
//constructing function
combination();
combination(const input_vector &);
……
};
调用如下:
#include "combination.H"#include <time.h>
#include <iostream.h>//using namespace std;void main()
{
combination<int>::input_vector iv;
combination<int>::output_vector ov;
iv.resize(11);
iv[0]=2;iv[1]=3;iv[2]=5;iv[3]=1;iv[4]=2;iv[5]=4;iv[6]=3;iv[7]=4;iv[8]=1;iv[9]=2;iv[10]=2; combination<int> c(iv); c[0][0]=3;c[0][1]=4;
c[1][0]=1;c[1][1]=34;c[1][2]=76;
c[2][0]=2;c[2][1]=3;c[2][2]=67;c[2][3]=34;c[2][4]=23;
c[3][0]=9;
c[4][0]=19;c[4][1]=29;
c[5][0]=90;c[5][1]=91;c[5][2]=94;c[5][3]=92;
c[6][0]=93;c[6][1]=59;c[6][2]=9;
c[7][0]=95;c[7][1]=9;c[7][2]=90;c[7][3]=95;
c[8][0]=950;
c[9][0]=39;c[9][1]=49;
c[10][0]=19;c[10][1]=569; while(c.have_next_combination())
{
c.get_next_combination(ov);
……
}
……
}运行报错:--------------------Configuration: template_produce_combination - Win32 Debug--------------------
Compiling...
combination.cpp
Linking...
main.obj : error LNK2001: unresolved external symbol "public: void __thiscall combination<int>::get_next_combination(class std::vector<int,class std::allocator<int> > &)const " (?get_next_combination@?$combination@H@@QBEXAAV?$vector@HV?$allocator@H@
std@@@std@@@Z)
main.obj : error LNK2001: unresolved external symbol "public: bool __thiscall combination<int>::have_next_combination(void)" (?have_next_combination@?$combination@H@@QAE_NXZ)
main.obj : error LNK2001: unresolved external symbol "public: int * __thiscall combination<int>::operator[](unsigned int const &)" (??A?$combination@H@@QAEPAHABI@Z)
main.obj : error LNK2001: unresolved external symbol "public: __thiscall combination<int>::combination<int>(class std::vector<unsigned int,class std::allocator<unsigned int> > const &)" (??0?$combination@H@@QAE@ABV?$vector@IV?$allocator@I@std@@@std@
@@Z)
Debug/template_produce_combination.exe : fatal error LNK1120: 4 unresolved externals
Error executing link.exe.template_produce_combination.exe - 5 error(s), 0 warning(s)

解决方案 »

  1.   

    可以看一下你的combination.cpp吗?
      

  2.   

    templates should be implemented in header files
      

  3.   

    你在Project->setting->link中添加上你用到的lib文件试试~~~~~~~~~
      

  4.   

    jiangsheng(蒋晟.Net)说的对,vc6的规矩,模板类都要写到头文件里,是这个原因的话
    别给分我
      

  5.   


    VC不支持模板函数导出的。在VC中,只你用到的模板函数才能被编译,否则不被编译最好将代码全部写到头文件中。
      

  6.   

    你不会把模板类的声明和定义分别写在.h和.cpp文件了吧,要是这样好像不行啊,Vc好像不支持这样的,你试着把定义和声明都写在.h文件里面看看
      

  7.   

    添加lib了吗?
    把程序发给我
    我帮你看看
    [email protected]
      

  8.   

    从你现在的代码看bool have_next_combination();
    void get_next_combination(output_vector &)const;这两个函数你是光声明,没有定义,所以连接时错误.