请看这个代码错在哪里?(此代码是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 上无法编译,请各位指点!
...全文
94 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
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
内容概要:本文详细介绍了利用Simulink进行变压器开路试验的电路连接配置与仿真实现方法,重点在于通过仿真手段还原实际电力系统中变压器在空载条件下的电气特性,从而深入理解其工作原理与性能表现。文作为电力系统仿真系列研究的一部分,系统阐述了从电路模型搭建、参数设定、仿真运行到结果分析的完整流程,突出展示了MATLAB/Simulink在电力设备建模与教学科研中的强大功能与应用价值。; 适合人群:具备电力系统基础知识,熟悉MATLAB/Simulink仿真环境,从事电气工程、自动化及相关领域的研发人员,以及高年级本科生和研究生。; 使用场景及目标:①掌握变压器开路试验的基本原理与Simulink仿真建模的具体步骤;②通过仿真实验深入理解空载电流、铁芯损耗及励磁特性等关键参数的物理意义;③为后续开展变压器短路试验、暂态过程分析以及其他电力设备的仿真研究奠定理论与实践基础。; 阅读建议:建议结合Simulink软件动手实践,逐步构建并调试电路模型,重点关注各元件参数的设置方法与测量模块的应用技巧,同时推荐参考文中提及的其他相关仿真案例进行拓展学习,以全面提升对电力系统仿真实践的整体认知与操作能力。

65,211

社区成员

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

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