请看这个代码错在哪里?(此代码是C++ Primer 3ed 第6章的源代码)

clark0506 2004-05-02 02:07:27
#include <algorithm>
#include <string>
#include <vector>
#include <utility>

#include <iostream>
#include <fstream>

#include <stddef>

extern vector<string,allocator> *retrieve_text();

int main()
{
vector<string,allocator> *text_file = retrieve_text();


cout << "----------- about to generate text read --------------\n";
ostream_iterator< string > output( cout, "\n" );
copy( text_file->begin(), text_file->end(), output );

return 0;
}

vector<string,allocator>*
retrieve_text()
{
string file_name;

cout << "please enter file name: ";
cin >> file_name;

ifstream infile( file_name.c_str(), ios::in );
if ( !infile ) {
cerr << "oops! unable to open file "
<< file_name << " -- bailing out!\n";
exit( -1 );
}
else cout << "\n";

vector<string,allocator> *lines_of_text = new vector<string,allocator>;
string textline;

typedef pair<string::size_type, int> stats;
stats maxline;
int linenum = 0;

while ( getline( infile, textline, '\n' ))
{
cout << "line read: " << textline << "\n";

if ( maxline.first < textline.length() )
{
maxline.first = textline.length();
maxline.second = linenum;
}

lines_of_text->push_back( textline );
linenum++;
}

cout << "\n";
cout << "number of lines: "
<< lines_of_text->size() << "\n";

cout << "maximum length: "
<< maxline.first << "\n";

cout << "longest line: "
<< (*lines_of_text)[ maxline.second ] << "\n";

return lines_of_text;
}



此代码是C++ Primer 3ed 第6章的源代码,得自侯先生的主页
据侯先生说,在他的win + VC6 上可以顺利通过编译

可惜偶的win2000+ vc6 上无法编译,请各位指点!
...全文
31 17 点赞 打赏 收藏 举报
写回复
17 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
clark0506 2004-05-04
表沉啊~

up
up

/|
  • 打赏
  • 举报
回复
古布 2004-05-03
去Dev-cpp下试一下!
  • 打赏
  • 举报
回复
antijpn 2004-05-03
换编译器吧!VC++6里面的编译器老了,VC++.Net2003里面完全没有这些问题
  • 打赏
  • 举报
回复
freefalcon 2004-05-03
identifier was truncated to '255' characters in the debug information
这个警告信息无关紧要,只是说标识符名字太长
嫌它烦的话,关掉它就行了
#pragma warning(disable: 4786)
  • 打赏
  • 举报
回复
lieyu063 2004-05-03
照antijpn(antijpn)的方法去做,结果有三个警告:
Compiling...
csdn.cpp
G:\编程\理解C++\数据类型\vector\未完成\csdn.cpp(24) : warning C4786: 'std::reverse_iterator,std::allocator > const *,std::basic_string,std::allocator >,std::basic_string,std::allocator > const &,std::basic_string,std::allocator > const *,int>' : identifier was truncated to '255' characters in the debug information
G:\编程\理解C++\数据类型\vector\未完成\csdn.cpp(24) : warning C4786: 'std::reverse_iterator,std::allocator > *,std::basic_string,std::allocator >,std::basic_string,std::allocator > &,std::basic_string,std::allocator > *,int>' : identifier was truncated to '255' characters in the debug information
d:\microsoft visual studio\vc98\include\vector(39) : warning C4786: 'std::vector,std::allocator >,std::allocator,std::allocator > > >::vector,std::allocator >,std::allocator,std::allocator > > >' : identifier was truncated to '255' characters in the debug information


  • 打赏
  • 举报
回复
clark0506 2004-05-03
谢谢 antijpn(antijpn)

按照你说的修改
我已经在vc.net 通过编译和运行

不过你能说一下出错的原因吗?
还有,生成那里有 --> 生成解决方案
--> 生成C++P6.cpp // 注:c++p6.cpp 是这个文件名
--> 编译

3个选项,好象每个都会进行编译, 谁能告诉我这3个有什么分别吗?

编译完之后怎么运行程序呢? 是不是通过 调试 --> 开始执行 ?
  • 打赏
  • 举报
回复
clark0506 2004-05-03
换编译器吧!VC++6里面的编译器老了,VC++.Net2003里面完全没有这些问题
//////////////////////////////////
引用自 antijpn(antijpn)
////////////////////////////////

偶在vc.net 上也做过了,还是一样的,不行啊。。。

  • 打赏
  • 举报
回复
legendsky 2004-05-02
等我看看
  • 打赏
  • 举报
回复
antijpn 2004-05-02
stddef改成cstddef

把,allocator全都去掉,再补充一下retrieve_text函数的定义就行了
  • 打赏
  • 举报
回复
clark0506 2004-05-02
不要沉啊~

继续up
  • 打赏
  • 举报
回复
clark0506 2004-05-02
upup

高人继续指点啊!
  • 打赏
  • 举报
回复
clark0506 2004-05-02
...

太奇怪了。。。

就算是stddef 是自己写的头文件,那我编译的结果应该也要
和楼上的一样啊。。。

可是我的结果。。。

无语,想不通。。。
  • 打赏
  • 举报
回复
qyet 2004-05-02
#include <algorithm>
#include <string>
#include <vector>
#include <utility>

#include <iostream>
#include <fstream>

#include <stddef>

using namespace std;//这句加不加效果一样
extern vector<string,allocator> *retrieve_text();

int main()
{
vector<string,allocator> *text_file = retrieve_text();


cout << "----------- about to generate text read --------------\n";
ostream_iterator< string > output( cout, "\n" );
copy( text_file->begin(), text_file->end(), output );

return 0;
}

vector<string,allocator>*
retrieve_text()
{
string file_name;

cout << "please enter file name: ";
cin >> file_name;

ifstream infile( file_name.c_str(), ios::in );
if ( !infile ) {
cerr << "oops! unable to open file "
<< file_name << " -- bailing out!\n";
exit( -1 );
}
else cout << "\n";

vector<string,allocator> *lines_of_text = new vector<string,allocator>;
string textline;

typedef pair<string::size_type, int> stats;
stats maxline;
int linenum = 0;

while ( getline( infile, textline, '\n' ))
{
cout << "line read: " << textline << "\n";

if ( maxline.first < textline.length() )
{
maxline.first = textline.length();
maxline.second = linenum;
}

lines_of_text->push_back( textline );
linenum++;
}

cout << "\n";
cout << "number of lines: "
<< lines_of_text->size() << "\n";

cout << "maximum length: "
<< maxline.first << "\n";

cout << "longest line: "
<< (*lines_of_text)[ maxline.second ] << "\n";

return lines_of_text;
}
--------------------------------------------------------------------
Compiling...
TestOnly.cpp
d:\mycpp\application\testonly.cpp(9) : fatal error C1083: Cannot open include file: 'stddef': No such file or directory
Error executing cl.exe.

TestOnly.obj - 1 error(s), 0 warning(s)

stddef应该是自己写的.h文件吧,这样看代码应该没什么问题。
我的是VC6.0 + Winxp:)
  • 打赏
  • 举报
回复
悠然红茶 2004-05-02
#include <stddef>指定的文件确定了吗?其内容是什么?
  • 打赏
  • 举报
回复
clark0506 2004-05-02
试过 还是没有效果。。。
  • 打赏
  • 举报
回复
新自由呼吸 2004-05-02
加 using namespace std;
  • 打赏
  • 举报
回复
clark0506 2004-05-02
编译出错信息如下:

--------------------Configuration: main0 - Win32 Debug--------------------
Compiling...
main0.C
d:\program files\microsoft visual studio\vc98\include\iosfwd(24) : error C2143: syntax error : missing '{' before '<'
d:\program files\microsoft visual studio\vc98\include\iosfwd(24) : error C2059: syntax error : '<'
d:\program files\microsoft visual studio\vc98\include\iosfwd(71) : error C2143: syntax error : missing '{' before '<'
d:\program files\microsoft visual studio\vc98\include\iosfwd(71) : error C2059: syntax error : '<'
d:\program files\microsoft visual studio\vc98\include\iosfwd(73) : error C2143: syntax error : missing '{' before '<'
d:\program files\microsoft visual studio\vc98\include\iosfwd(73) : error C2059: syntax error : '<'
d:\program files\microsoft visual studio\vc98\include\iosfwd(74) : error C2061: syntax error : identifier 'wstreampos'
d:\program files\microsoft visual studio\vc98\include\iosfwd(74) : error C2059: syntax error : ';'
d:\program files\microsoft visual studio\vc98\include\iosfwd(76) : error C2143: syntax error : missing '{' before '<'
d:\program files\microsoft visual studio\vc98\include\iosfwd(76) : error C2059: syntax error : '<'
d:\program files\microsoft visual studio\vc98\include\iosfwd(137) : error C2143: syntax error : missing '{' before '<'
d:\program files\microsoft visual studio\vc98\include\iosfwd(137) : error C2059: syntax error : '<'
d:\program files\microsoft visual studio\vc98\include\iosfwd(176) : error C2143: syntax error : missing '{' before '<'
d:\program files\microsoft visual studio\vc98\include\iosfwd(176) : error C2059: syntax error : '<'
d:\program files\microsoft visual studio\vc98\include\iosfwd(215) : error C2143: syntax error : missing '{' before '<'
d:\program files\microsoft visual studio\vc98\include\iosfwd(215) : error C2059: syntax error : '<'
d:\program files\microsoft visual studio\vc98\include\iosfwd(217) : error C2061: syntax error : identifier 'ios_base'
d:\program files\microsoft visual studio\vc98\include\iosfwd(217) : error C2059: syntax error : ';'
d:\program files\microsoft visual studio\vc98\include\iosfwd(218) : error C2143: syntax error : missing '{' before '<'
d:\program files\microsoft visual studio\vc98\include\iosfwd(218) : error C2059: syntax error : '<'
d:\program files\microsoft visual studio\vc98\include\iosfwd(220) : error C2143: syntax error : missing '{' before '<'
d:\program files\microsoft visual studio\vc98\include\iosfwd(220) : error C2059: syntax error : '<'
d:\program files\microsoft visual studio\vc98\include\iosfwd(222) : error C2143: syntax error : missing '{' before '<'
d:\program files\microsoft visual studio\vc98\include\iosfwd(222) : error C2059: syntax error : '<'
d:\program files\microsoft visual studio\vc98\include\iosfwd(224) : error C2143: syntax error : missing '{' before '<'
d:\program files\microsoft visual studio\vc98\include\iosfwd(224) : error C2059: syntax error : '<'
d:\program files\microsoft visual studio\vc98\include\iosfwd(226) : error C2143: syntax error : missing '{' before '<'
d:\program files\microsoft visual studio\vc98\include\iosfwd(226) : error C2059: syntax error : '<'

……

main0.obj - 102 error(s), 0 warning(s)


郁闷,实在是闹不懂啊,偶是前些日子才开始学的 C++ ,
是个菜青虫,所以请大家多多关照啊~

thanks all
  • 打赏
  • 举报
回复
相关推荐
发帖
C++ 语言
加入

6.0w+

社区成员

C++ 语言相关问题讨论,技术干货分享,前沿动态等
申请成为版主
帖子事件
创建了帖子
2004-05-02 02:07
社区公告
暂无公告