C++编译错误,高手帮忙解决啊~~~

liuqiker 2008-03-28 12:00:31
在加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);
}
...全文
69 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
liuqiker 2008-03-28
  • 打赏
  • 举报
回复
出现的错误的地方就是那几个typedef,而且是全错了,每个错误都是很长的一句话,我也看不大明白,好像是跟名字空间有关的。 一共有45个error呢。
是不像用map的,只是想练习一下vector,故意那样写的。
谢谢你咯。
可是我到底错在哪呢?
对了,VC编译出错误以后,我双击那些错误,就跳转到vector.h里面了
指向vector.h里面的typedef
比如这几句 class vector {
public:
typedef vector<_Ty, _A> _Myt;
typedef _A allocator_type;
typedef _A::size_type size_type;
typedef _A::difference_type difference_type;
typedef _A::pointer _Tptr;
typedef _A::const_pointer _Ctptr;
typedef _A::reference reference;
typedef _A::const_reference const_reference;
typedef _A::value_type value_type;
typedef _Tptr iterator;
typedef _Ctptr const_iterator;
typedef reverse_iterator<const_iterator, value_type,
const_reference, _Ctptr, difference_type>
const_reverse_iterator;
typedef reverse_iterator<iterator, value_type,
reference, _Tptr, difference_type>
reverse_iterator;
...
错误会指向上边这些typedef的。
xbt746 2008-03-28
  • 打赏
  • 举报
回复
vector<location>不是vector<pair>吗
??干嘛不用map
还有你把错误代码的行数用不一样的颜色标出来吧
看半天看不清楚啊
xbt746 2008-03-28
  • 打赏
  • 举报
回复
你就把Pair注销掉试试看
ryfdizuo 2008-03-28
  • 打赏
  • 举报
回复
不要用pair结果,如果只是位置的话,直接自己定义一个struct就可以;
struct MyPair
{
int x;
iny;
MyPair(int x1 ,int y1)
: x(x1), y(y1)
{}
};
至于make_pair函数你可以使用构造函数代替;

64,654

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧