我试图使用bitset。如果所有代码都放在单个.cc文件中,则代码可以正常工作。但使用多个文件将无法正常工作。考虑具有三个文件的简单代码:UTY_Common.h,UTY_Common.cc和MEMD_Main.cc。
UTY_Common.h
#ifndef UTY_COMMON_H
#define UTY_COMMON_H
#include <bitset>
template<unsigned int T>
unsigned int getLength( const std::bitset<T>& bitvec );
#endif
UTY_Common.cc
#include "UTY_Common.h"
template<unsigned int T>
unsigned int getLength( const std::bitset<T>& bitvec )
{
unsigned int len = 0;
for ( unsigned int j = 0; j < bitvec.size(); j++ )
{
if ( bitvec[j] ) ++len;
}
return len;
}
MEMD_Main.cc
#include <iostream>
#include <bitset>
#include "UTY_Common.h"
#define N 1001
int main()
{
std::bitset<N> temp;
std::cout << getLength(temp);
return 0;
}
我在VS2010中遇到以下错误。
错误1错误LNK2019:未解析的外部符号“unsigned int __cdecl getLength&lt; 1001&gt;(类std :: bitset&lt; 1001&gt; const&amp;)”(?? $ getLength @ $ 0DOJ @@@ YAIABV?$ bitset @ $ 0DOJ @@ std @@@ Z)在函数_main C:\ Users \ anis \ Documents \ Visual Studio 2010 \ Projects \ MEMD \ MEMD \ MEMD_Main.obj
错误2错误LNK1120:1个未解析的外部C:\ Users \ anis \ Documents \ Visual Studio 2010 \ Projects \ MEMD \ Debug \ MEMD.exe
我也在cygwin上尝试了g ++,得到了类似的链接错误。