急!!!高分求教C++高手!!!

中医程序猿 2002-10-21 09:45:59
按照书上打的程序为什么不能通过编译?
//文本查询系统,摘自[Lippman & 潘爱民02]P259
#include <algorithm>
#include <string>
#include <vector>
#include <utility>
#include <map>
#include <set>
#include <fstream.h>
#include <stddef.h>
#include <ctype.h>
typedef pair<short,short> location;
typedef vector<location, allocator> loc;
typedef vector<string, allocator> text;
typedef pair<text*, loc*> text_loc;

class TextQuery{
public:
TextQuery() { memset(this, 0, sizeof(TextQuery)); }
static void filter_elements(string felems) { filt_elems = felems; }
void query_text();
void display_map_text();
void display_text_locations();
void doit(){
retrieve_text();
separate_words();
filter_text();
suffix_text();
strip_caps();
build_word_map();
}
private:
void retrieve_text();
void separate_words();
void filter_text();
void strip_caps();
void suffix_text();
void suffix_s(string&);
void build_word_map();
private:
vector<string, alloctar> *lines_of_text;
text_loc *text_locations;
map<string, loc*, less<string>, allocatar> *word_map;
static string filt_elems;
};
string TextQuery::filt_elems("\",.;!?)(\\/");
int main()
{
TextQuery tq;
tq.doit();
tq.query_text();
tq.display_map_text();
}
void TextQuery::retrieve_text()
{//由于字数限制,函数实现部分省略
//源程序请见潘爱民译《C++ Primer中文版》第259页
...
}
void TextQuery::separate_words()
{...}
void TextQuery::filter_text()
{...}
void TextQuery::suffix_text()
{ ... }
void TextQuery::suffix_s(string &word)
{...}
void TextQuery::strip_caps()
{...}
void TextQuery::build_word_map()
{...}
void TextQuery::query_text()
{...}
void TextQuery::display_map_text()
{...}
我的系统Win2000 Advance Server + VC6
...全文
192 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
kwok_1980 2002-10-21
  • 打赏
  • 举报
回复
//就你所给出的程序,我帮你改了,大概没有什么问题.
//主要是allocator的问题,把你的程序有allocator的地方都去掉.
//这个是旧的编译器用的内存分配管理器.它是默认的,不用写出来.
//可能有些编译器不支持.
//还有就是text这个别名.在VC下有同名的变量所以要该一下(在函数库中有
//VC同名的).
//至于还有没有其它的隐含错误.由于没有实现代码,所以现在是看不到啦.

#include <algorithm>
#include <string>
#include <vector>
#include <utility>
#include <map>
#include <set>
#include <fstream.h>
#include <stddef.h>
#include <ctype.h>
using namespace std;//<--------------------

typedef pair<short,short> location;
typedef vector<location> loc; //----------------
typedef vector<string> textA; //---------------------
typedef pair<textA*, loc* > text_loc;

class TextQuery{
public:
TextQuery() { memset(this, 0, sizeof(TextQuery)); }
static void filter_elements(string felems) { filt_elems = felems; }
void query_text();
void display_map_text();
void display_text_locations();
void doit(){
retrieve_text();
separate_words();
filter_text();
suffix_text();
strip_caps();
build_word_map();
}
private:
void retrieve_text();
void separate_words();
void filter_text();
void strip_caps();
void suffix_text();
void suffix_s(string&);
void build_word_map();
private:
vector<string> *lines_of_text; //<--------------------
text_loc *text_locations;
map<string, loc*, less<string> > *word_map;//<----------------
static string filt_elems;
};
string TextQuery::filt_elems("\",.;!?)(\\/");
int main()
{/*
TextQuery tq;
tq.doit();
tq.query_text();
tq.display_map_text();
*/
return 0;
}
kwok_1980 2002-10-21
  • 打赏
  • 举报
回复
#include <algorithm>
#include <string>
#include <vector>
#include <utility>
#include <map>
#include <set>
#include <fstream.h>
#include <stddef.h>
#include <ctype.h>
using namespace std; //<--------这里加

至于错误更多,是因为那些都是真正你看到的错误.
如果因为错误多就放到其它地方,那些错误只是暂时没有检查出来罢了.
中医程序猿 2002-10-21
  • 打赏
  • 举报
回复
在main()里加入using namespace std;后仍有错误,在main()外加入using namespace std;错误更多。
但是,将typedef(s)系列语句加入main()后,不再出现error C2143: syntax error : missing ';' before '<'报错,然而还是通不过,出现
--------------------Configuration: TRS - Win32 Debug--------------------
Compiling...
TRS.CPP
F:\Programming Work\TRS\TRS.CPP(28) : error C2061: syntax error : identifier 'string'
F:\Programming Work\TRS\TRS.CPP(46) : error C2061: syntax error : identifier 'string'
F:\Programming Work\TRS\TRS.CPP(50) : error C2143: syntax error : missing ';' before '<'
F:\Programming Work\TRS\TRS.CPP(50) : error C2501: 'vector' : missing storage-class or type specifiers
F:\Programming Work\TRS\TRS.CPP(50) : error C2059: syntax error : '<'
F:\Programming Work\TRS\TRS.CPP(50) : error C2238: unexpected token(s) preceding ';'
F:\Programming Work\TRS\TRS.CPP(51) : error C2143: syntax error : missing ';' before '*'
F:\Programming Work\TRS\TRS.CPP(51) : error C2501: 'text_loc' : missing storage-class or type specifiers
F:\Programming Work\TRS\TRS.CPP(51) : error C2501: 'text_location' : missing storage-class or type specifiers
F:\Programming Work\TRS\TRS.CPP(52) : error C2143: syntax error : missing ';' before '<'
F:\Programming Work\TRS\TRS.CPP(52) : error C2501: 'map' : missing storage-class or type specifiers
F:\Programming Work\TRS\TRS.CPP(52) : error C2059: syntax error : '<'
F:\Programming Work\TRS\TRS.CPP(52) : error C2238: unexpected token(s) preceding ';'
F:\Programming Work\TRS\TRS.CPP(53) : error C2146: syntax error : missing ';' before identifier 'filt_elems'
F:\Programming Work\TRS\TRS.CPP(53) : error C2501: 'filt_elems' : missing storage-class or type specifiers
F:\Programming Work\TRS\TRS.CPP(56) : error C2039: 'filt_elems' : is not a member of 'TextQuery'
F:\Programming Work\TRS\TRS.CPP(24) : see declaration of 'TextQuery'
F:\Programming Work\TRS\TRS.CPP(56) : error C2146: syntax error : missing ';' before identifier 'filt_elems'
F:\Programming Work\TRS\TRS.CPP(56) : error C2501: 'string' : missing storage-class or type specifiers
F:\Programming Work\TRS\TRS.CPP(56) : fatal error C1004: unexpected end of file found
Error executing cl.exe.
TRS.OBJ - 19 error(s), 0 warning(s)
ddmpqcw 2002-10-21
  • 打赏
  • 举报
回复
要用using namespace std

你试试看!@
sdwtao 2002-10-21
  • 打赏
  • 举报
回复
估计就是名字空间问题 类型找不到定义一般会报你的那些个错误
中医程序猿 2002-10-21
  • 打赏
  • 举报
回复
MSCLUB兄,能不能具体说说template在这个程序里怎么用?
MSCLUB 2002-10-21
  • 打赏
  • 举报
回复
在vc中,typdef好像不支持参数
建议用模版!
template<a,b>的形式
MSCLUB 2002-10-21
  • 打赏
  • 举报
回复
以下是错误提示:
:\test\test.cpp(11) : error C2143: syntax error : missing ';' before '<'
e:\test\test.cpp(11) : error C2143: syntax error : missing ';' before '<'
e:\test\test.cpp(12) : error C2143: syntax error : missing ';' before '<'
e:\test\test.cpp(12) : error C2143: syntax error : missing ';' before '<'
e:\test\test.cpp(13) : warning C4091: 'typedef ' : ignored on left of 'int' when no variable is declared
e:\test\test.cpp(13) : error C2143: syntax error : missing ';' before '<'
e:\test\test.cpp(13) : error C2143: syntax error : missing ';' before '<'
e:\test\test.cpp(14) : warning C4091: 'typedef ' : ignored on left of 'int' when no variable is declared
e:\test\test.cpp(14) : error C2143: syntax error : missing ';' before '<'
e:\test\test.cpp(14) : error C2143: syntax error : missing ';' before '<'
e:\test\test.cpp(19) : error C2061: syntax error : identifier 'string'
e:\test\test.cpp(37) : error C2061: syntax error : identifier 'string'
e:\test\test.cpp(40) : error C2059: syntax error : '<'
e:\test\test.cpp(40) : error C2238: unexpected token(s) preceding ';'
e:\test\test.cpp(41) : error C2143: syntax error : missing ';' before '*'
e:\test\test.cpp(41) : error C2501: 'text_loc' : missing storage-class or type specifiers
e:\test\test.cpp(41) : error C2501: 'text_locations' : missing storage-class or type specifiers
e:\test\test.cpp(42) : error C2143: syntax error : missing ';' before '<'
e:\test\test.cpp(42) : error C2501: 'map' : missing storage-class or type specifiers
e:\test\test.cpp(42) : error C2059: syntax error : '<'
e:\test\test.cpp(42) : error C2238: unexpected token(s) preceding ';'
e:\test\test.cpp(43) : error C2146: syntax error : missing ';' before identifier 'filt_elems'
e:\test\test.cpp(43) : error C2501: 'filt_elems' : missing storage-class or type specifiers
e:\test\test.cpp(45) : error C2039: 'filt_elems' : is not a member of 'TextQuery'
e:\test\test.cpp(16) : see declaration of 'TextQuery'
e:\test\test.cpp(45) : error C2146: syntax error : missing ';' before identifier 'filt_elems'
e:\test\test.cpp(45) : error C2501: 'string' : missing storage-class or type specifiers
e:\test\test.cpp(45) : fatal error C1004: unexpected end of file found
Error executing cl.exe.

test.exe - 25 error(s), 2 warning(s)
johns78 2002-10-21
  • 打赏
  • 举报
回复
为什么 不注意一下namespace的问题?
这个内容用起来是很简单的,只是理解上有点难,不过别怕,
名子空间嘛,好好想想。
sjf331 2002-10-21
  • 打赏
  • 举报
回复
对呀,总要说一下错误提示吧?
Cauty45 2002-10-21
  • 打赏
  • 举报
回复
错误提示先
MSCLUB 2002-10-21
  • 打赏
  • 举报
回复
问一下:VC环境是不是都按巨模式编译程序的?
ywchen2000 2002-10-21
  • 打赏
  • 举报
回复
学习
中医程序猿 2002-10-21
  • 打赏
  • 举报
回复
我明白了,谢谢大家。

69,382

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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