C++编译错误,高手帮忙解决啊~~~
在加typedef之前程序是通过编译的,而且工作也正常,但是加了几个typedef并把相关变量替换之后就出现许多类似“F:\temp\Finder\test.cpp(131) : see reference to class template instantiation 'std::vector<class std::vector<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,class std::allocator<class std::basic_string<
char,struct std::char_traits<char>,class std::allocator<char> > > > *,class std::vector<struct std::pair<int,int>,class std::allocator<struct std::pair<int,int> > > *>' being compiled”的错误。
请高手指点一下,我到底哪些地方写错了?谢谢!
源代码如下:
#include <string>
#include <fstream>
#include <iostream>
#include <vector>
#include <utility>
#include <locale>
using namespace std;
void strip_cap(vector<string> * words);
typedef pair<int , int> location;
typedef vector<location> loc;
typedef vector<string> ftext;
typedef vector<ftext * , loc *> ftext_loc;
ftext_loc * seperate_words(const vector<string> * text_file) //分离单词
{
int line_num = 0 , word_num = 0;
int word_num_in_line = 0;
// string text_line;
vector<string> * words = new vector<string>;
vector<location> * word_location = new vector<location>;
for (line_num = 0 ; line_num < text_file->size() ; line_num ++)
{
string::size_type pos = 0 , prev = 0;
string::size_type interpunction_index = 0;
string interpunction = ",.?!;!!:'=<>";
string text_line = (* text_file)[line_num];
while ((interpunction_index = text_line.find_first_of(interpunction , interpunction_index)) != string::npos)
{
text_line.erase(interpunction_index , 1); //去掉文本里的标点符号
interpunction_index++;
}
while ((pos = text_line.find_first_of(' ' , pos)) != string::npos) //分离出每个单词
{
words->push_back(text_line.substr(prev , pos - prev));
word_num_in_line++;
word_location->push_back(make_pair(line_num+1 , word_num_in_line)); //单词的行列位置
strip_cap(words); //大写换小写
prev = ++pos;
word_num++;
cout<<"The "<<word_num_in_line<<" word in line "<<line_num+1<<" is : "<<(*words)[word_num - 1]<<endl;
}
words->push_back(text_line.substr(prev , pos - prev)); //最后一个单词
word_num_in_line += 1;
word_location->push_back(make_pair(line_num+1 , word_num_in_line)); //最后一个单词的位置
strip_cap(words);
word_num++;
word_num_in_line = 0;
cout<<"The last word in line "<<line_num + 1<<" is : "<<(*words)[word_num - 1]<<endl;
}
return new ftext_loc(words , word_location);
}
vector <string> * retrieve_text() //读取文本
{
string file_name;
cout<<"please enter the file name :";
cin>>file_name;
ifstream infile(file_name.c_str() , ios::in);
if (!infile)
{
cerr<<"oops! unable to open files : "
<<file_name<<"--bailing out"
<<endl;
exit(-1);
}
else
cout<<"\n";
vector <string> * lines_of_text = new vector <string> ;
string textline;
typedef pair<string::size_type , int> status;
status maxline;
int line_num = 1;
while (getline(infile , textline , '\n'))
{
cout<<"line read : "<<textline<<'\n';
if (maxline.first < textline.size())
{
maxline.first = textline.size();
maxline.second = line_num;
}
lines_of_text->push_back(textline);
line_num++;
}
cout<<"max size of every lines is : "<<maxline.first<<'\n';
cout<<"there are "<<line_num<<" line all together ."<<'\n';
return lines_of_text;
}
void strip_cap(vector <string> * words)
{
string cap("ABCDEFGHIJKLMNOPQRSTUVWXYZ");
vector<string>::iterator iter = words->begin();
while (iter != words->end())
{
string::size_type pos = 0;
while((pos = (*iter).find_first_of(cap , pos)) != string::npos)
(*iter)[pos] = tolower((*iter)[pos]);
iter++;
}
}
void main()
{
vector<string> * example = new vector<string>;
example = retrieve_text();
seperate_words(example);
}