【发布时间】:2014-05-14 14:56:24
【问题描述】:
谁能告诉我为什么会出现这个错误。
模板头文件(prog.h):
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <functional>
using namespace std;
#ifndef H_PROG
#define H_PROG
// data files
#define D1 "data1.txt"
#define INT_SZ 4 // width of integer
#define FLT_SZ 7 // width of floating-pt number
#define STR_SZ 12 // width of string
#define INT_LN 15 // no of integers on single line
#define FLT_LN 9 // no of floating-pt nums on single line
#define STR_LN 5 // no of strings on single line
// function and class prototypes
// stores items from input file into vector
template < class T >
void get_list ( vector < T >&, const char* );
#endif
.cpp 文件:
#include "prog.h"
#include <fstream>
template < class T >
void get_list ( vector < T >& vector1, const char* filename )
{
ifstream infile;
ofstream outfile;
infile.open(filename);
if(infile.fail())
{
cout<<"File did not open."<<endl;
}
else
{
T data;
while(infile)
{
infile>>data;
vector1.insert(data);
}
}
}
在我的主要功能中:
#include "prog.h"
#include <conio.h>
int main ( )
{
vector < int > v1;
vector < float > v2;
vector < string > v3;
// first heap
cout << "first heap - ascending order:\n\n";
get_list(v1,D1);
_getch();
return 0;
}
当我尝试运行它时,我收到一条错误消息:
错误 LNK2019: 无法解析的外部符号 "void __cdecl get_list(class std::vector > &,char const *)" (??$get_list@H@@YAXAAV?$vector@HV?$allocator@H@std@ @@std@@PBD@Z) 在函数 _main 中引用
【问题讨论】: