LNK2019: 无法解析的外部符号
#include <fstream>#include <iostream>
#include <cstdlib>
#include <string>
#include <vector>
#include <set>
#include <map>
#pragma comment(lib,"ws2_32.lib")
using namespace std;
class TextQuery
{
public:
typedef std::vector<std::string>::size_type line_no;
void read_file( std::ifstream &is )
{
store_file(is);
build_map();
}
std::set<line_no> run_query(const std::string & ) const;
std::string text_line(line_no) const;
private:
void store_file(std::ifstream&);
void build_map();
std::vector<std::string>lines_of_text;
std::map< std::string, std::set<line_no> > word_map;
};
string make_plural(size_t ctr, const string &word, const string &ending);
void print_results(const set<TextQuery::line_no>& locs,const string& sought,const TextQuery &file);
ifstream& open_file(ifstream &in,const string &file);
string make_plural(size_t ctr, const string &word, const string &ending)
{
return(ctr == 1)? word:word+ending;
}
void print_results(const set<TextQuery::line_no>& locs,const string& sought,const TextQuery &file)
{
typedef set<TextQuery::line_no>line_nums;
line_nums::size_type size=locs.size();
cout << "\n" << sought << "occours" <<size << " " <<make_plural(size,"time", "s") << endl;
line_nums::const_iterator it = locs.begin();
for(;it != locs.end();++it)
{
cout << "\t(line" << (*it)+1 << ")" << file.text_line(*it) <<endl;
}
}
ifstream& open_file(ifstream &in,const string &file)
{
in.close();
in.clear();
in.open(file.c_str());
return in;
}
int main(int argc,char **argv)
{
ifstream infile;
if(argc < 2|| !open_file(infile,argv[1]))
{
cerr << "No input file!" << endl;
return EXIT_FAILURE;
}
TextQuery tq;
tq.read_file(infile);
while(true){
cout << "enter word to look for, or q to quit :";
string s;
cin >>s;
if (!cin || s=="q")break;
set<TextQuery::line_no>locs = tq.run_query(s);
print_results(locs,s,tq);
}
return 0;
}
1>------ 已启动生成: 项目: TextQuery, 配置: Debug Win32 ------
1>生成启动时间为 2011/6/21 19:08:02。
1>InitializeBuildStatus:
1> 正在对“Debug\TextQuery.unsuccessfulbuild”执行 Touch 任务。
1>ClCompile:
1> main.cpp
1>ManifestResourceCompile:
1> 所有输出均为最新。
1>main.obj : error LNK2019: 无法解析的外部符号 "public: class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > __thiscall TextQuery::text_line(unsigned int)const " (?text_line@TextQuery@@QBE?AV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@I@Z),该符号在函数 "void __cdecl print_results(class std::set<unsigned int,struct std::less<unsigned int>,class std::allocator<unsigned int> > const &,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &,class TextQuery const &)" (?print_results@@YAXABV?$set@IU?$less@I@std@@V?$allocator@I@2@@std@@ABV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@2@ABVTextQuery@@@Z) 中被引用
1>main.obj : error LNK2019: 无法解析的外部符号 "public: class std::set<unsigned int,struct std::less<unsigned int>,class std::allocator<unsigned int> > __thiscall TextQuery::run_query(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &)const " (?run_query@TextQuery@@QBE?AV?$set@IU?$less@I@std@@V?$allocator@I@2@@std@@ABV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@3@@Z),该符号在函数 _main 中被引用
1>main.obj : error LNK2019: 无法解析的外部符号 "private: void __thiscall TextQuery::build_map(void)" (?build_map@TextQuery@@AAEXXZ),该符号在函数 "public: void __thiscall TextQuery::read_file(class std::basic_ifstream<char,struct std::char_traits<char> > &)" (?read_file@TextQuery@@QAEXAAV?$basic_ifstream@DU?$char_traits@D@std@@@std@@@Z) 中被引用
1>main.obj : error LNK2019: 无法解析的外部符号 "private: void __thiscall TextQuery::store_file(class std::basic_ifstream<char,struct std::char_traits<char> > &)" (?store_file@TextQuery@@AAEXAAV?$basic_ifstream@DU?$char_traits@D@std@@@std@@@Z),该符号在函数 "public: void __thiscall TextQuery::read_file(class std::basic_ifstream<char,struct std::char_traits<char> > &)" (?read_file@TextQuery@@QAEXAAV?$basic_ifstream@DU?$char_traits@D@std@@@std@@@Z) 中被引用
1>c:\users\kkk\documents\visual studio 2010\Projects\TextQuery\Debug\TextQuery.exe : fatal error LNK1120: 4 个无法解析的外部命令
1>
1>生成失败。
1>
1>已用时间 00:00:01.28
========== 生成: 成功 0 个,失败 1 个,最新 0 个,跳过 0 个 ==========
完全不明白啊。。。。。确实是直接用的c++ primer上的代码为什么会出现这种错误呢?