类的使用出现错误,
主源代码:复制内容到剪贴板
代码:
#include<iostream>
#include"TextQuery.hpp"
#include<set>
#include<map>
#include<fstream>
#include<string>
#include<sstream>
using namespace std;
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<<"occurs"<<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 avgc,char**argv){
ifstream infile;
if(avgc<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 loof for,orq to quit:";
string xx;
cin>>xx;
if(!cin||xx=="q")
break;
set<TextQuery::line_no>locs=tq.run_query(xx);
print_results(locs,xx,tq);
}
return 0;
}
定义类的源代码:
复制内容到剪贴板
代码:
#include<iostream>
#include<set>
#include<map>
#include<fstream>
#include<string>
#include<sstream>
#include<vector>
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;};
类外定义的函数代码:
复制内容到剪贴板
代码:
#include<iostream>
#include<set>
#include<map>
#include<fstream>
#include<string>
#include"TextQuery.hpp"
#include<sstream>
using namespace std;
void TextQuery::store_file(ifstream &is)
{
string s;
while(getline(is,s))
lines_of_text.push_back(s);
}
void TextQuery::build_map(){
for(line_no ix=0;ix!=lines_of_text.size();++ix){
isringstream line(lines_of_text[ix]);
string word;
while(line>>word)
word_map[word].insert(ix) ;
}
}
set<TextQuery::line_no>TextQuery::run_query(const string &query_word)const{
map<string,set<line_no> >::const_iterator loc=word_map.find(query_word);
if(loc==word_map.end())
return set<line_no>();
else
return loc->second;
}
string TextQuery::text_line(line_no line)const{
if(line<lines_of_text.size())
return lines_of_text[line];
throw std::out_of_range("line number out of range");
}
我用C-free编译的
错误信息::
--------------------配置: mingw5 - CUI Debug, 编译器类型: MinGW--------------------
检查文件依赖性...
正在编译 C:\Users\sundance\Desktop\新建文件夹\main.cpp...
[Warning] C:\Users\sundance\Desktop\新建文件夹\TextQuery.hpp:26:52: warning: no newline at end of file
[Warning] C:\Users\sundance\Desktop\新建文件夹\main.cpp:50:2: warning: no newline at end of file
正在连接...
[Error] C:\Users\sundance\Desktop\新建文件夹\main.cpp:21: undefined reference to `TextQuery::text_line(unsigned int) const'
[Error] C:\Users\sundance\Desktop\新建文件夹\main.cpp:45: undefined reference to `TextQuery::run_query(std::string const&) const'
[Error] E:\C-FREE~1\mingw\include\C__~1\348B92~1.5\bits\stl_tree.h:(.text$_ZN9TextQuery9read_fileERSt14basic_ifstreamIcSt11char_traitsIcEE[TextQuery::read_file(std::basic_ifstream<char, std::char_traits<char> >&)]+0x14): undefined reference to `TextQuery::store_file(std::basic_ifstream<char, std::char_traits<char> >&)'
[Error] E:\C-FREE~1\mingw\include\C__~1\348B92~1.5\bits\stl_tree.h:(.text$_ZN9TextQuery9read_fileERSt14basic_ifstreamIcSt11char_traitsIcEE[TextQuery::read_file(std::basic_ifstream<char, std::char_traits<char> >&)]+0x1f): undefined reference to `TextQuery::build_map()'
[Error] collect2: ld returned 1 exit status
构建中止 main: 5 个错误, 2 个警告