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

liuqiker 2008-03-28 11:48:49
在加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);
}
...全文
118 11 打赏 收藏 举报
写回复
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
liuqiker 2008-03-28
  • 打赏
  • 举报
回复
typedef vector <ftext * , loc *> ftext_loc; 是错了
刚才没注意看,直接复制粘贴的
原来程序里面是typedef pair <ftext * , loc *> ftext_loc;的
不好意思
粘贴的时候弄错了
但是是typedef pair <ftext * , loc *> ftext_loc;的时候确实会出现很多我说的那种错误的
liuqiker 2008-03-28
  • 打赏
  • 举报
回复
对了,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的。
ttkk_2007 2008-03-28
  • 打赏
  • 举报
回复
typedef vector <ftext * , loc *> ftext_loc;
==================================
这句对了?不可能把
liuqiker 2008-03-28
  • 打赏
  • 举报
回复
出现的错误的地方就是那几个typedef,而且是全错了,没个错误都是很长的一句话,我也看不大明白,好像是跟名字空间有关的。
typedef vector <ftext * , loc *> ftext_loc; 语法应该没错的,我看C++primer上也是这样写的啊,是错了么?
还有我不是想用map,只是为了联系vector才这样写的。
以后我贴代码会注意的。
谢谢你咯。
0黄瓜0 2008-03-28
  • 打赏
  • 举报
回复
C++使用模板容器就是让你尽量远离指针,但你在可以不用指针的地方引入指针.
0黄瓜0 2008-03-28
  • 打赏
  • 举报
回复
typedef vector<ftext * , loc *> ftext_loc;

这句不对吧.你是不是想使用map或set
0黄瓜0 2008-03-28
  • 打赏
  • 举报
回复
以后贴代码请贴在这儿
  • 打赏
  • 举报
回复
#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);
}
txwcan999 2008-03-28
  • 打赏
  • 举报
回复
程序的内存释放是个大问题。。。
txwcan999 2008-03-28
  • 打赏
  • 举报
回复

当我改成下面的就没问题了
typedef pair <ftext * , loc *> ftext_loc;

编译通过。

不过new 没有delete

不知楼主的为什么没过?
txwcan999 2008-03-28
  • 打赏
  • 举报
回复
typedef vector <ftext * , loc *> ftext_loc; //这句话有问题, 不能构造

相关推荐
发帖
C++ 语言

6.3w+

社区成员

C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
帖子事件
创建了帖子
2008-03-28 11:48
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下