C和C++代码精粹的问题

meetyu 2009-02-25 02:14:15
//下面是<<C和C++代码精粹>>上的一条程序,但是在VC6编译不过.
#include <iostream>

using namespace std;

int main()
{
const size_t BUFSIZ = 128;
char s[BUFSIZ];
while (cin.getline(s,BUFSIZ))
cout << s << '\n';
return 0;
}

//VC6这样报差

F:\try\copy.cpp(7) : warning C4091: '' : ignored on left of 'const unsigned int' when no variable is declared
F:\try\copy.cpp(7) : error C2143: syntax error : missing ';' before 'constant'
F:\try\copy.cpp(7) : error C2106: '=' : left operand must be l-value
Error executing cl.exe.

copy.obj - 2 error(s), 1 warning(s)

想不明白,希望大家解释下,为什么会这样
...全文
135 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
csjtxy 2009-02-25
  • 打赏
  • 举报
回复
全局变量 
waizqfor 2009-02-25
  • 打赏
  • 举报
回复
BUFSIZ is the required user-allocated buffer for the setvbuf routine
  • 打赏
  • 举报
回复
原来BUFSIZ 跟全局宏冲突...难怪.
xianyuxiaoqiang 2009-02-25
  • 打赏
  • 举报
回复
可见书上写死的代码经常出问题。
xianyuxiaoqiang 2009-02-25
  • 打赏
  • 举报
回复
顶。建议变量不要起这么特殊的名字。
yangch_nhcmo 2009-02-25
  • 打赏
  • 举报
回复

#include <iostream>

using namespace std;

int main()
{
//const size_t BUFSIZ = 128; //BUFSIZ与系统中定义的BUFSIZ 相冲突,BUFSIZ 是系统定义的全局变量
const size_t BUFSIZ_1 = 128;
char s[BUFSIZ_1]; //BUFSIZ==> BUFSIZ_1
while (cin.getline(s,BUFSIZ_1)) //BUFSIZ==> BUFSIZ_1
cout << s << '\n';
return 0;
}

brookmill 2009-02-25
  • 打赏
  • 举报
回复
我用cl -E test.cpp > test.i看了预处理的结果,是这样的:
#line 2 "test.cpp"

using namespace std;

int main()
{
const size_t 512 = 128;
char s[512];
while (cin.getline(s,512))
cout << s << '\n';
return 0;
}

也就是说,BUFSIZ被宏替换成512了
brookmill 2009-02-25
  • 打赏
  • 举报
回复
我怀疑这个BUFSIZ有某些特殊的用途,就像关键字
随便改成别的什么,比如BUFSIZEEE,就可以了

33,311

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 新手乐园
社区管理员
  • 新手乐园社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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