'rand' 的问题,6.0里面是不是没有这个函数

angelcm51 2008-02-08 10:36:57
#include <iostream>
#include <cstdlib>
#include <ctime>

using std::cout;
using std::endl;
using std::rand;
using std::srand;
using std::time;

int main() {
const int limit1 = 500; //Upper limit for on set of random values
const int limit2 = 31; //Upper limit for another set of values

cout << "First we will use the default sequence from rand(). \n ";
cout << "Three random integer from 0 to " << RAND_MAX << ": "
<< rand() << " " << rand() << " " <<rand() << endl;
cout << endl << "Now we will use a new seed for rand().\n ";
srand((unsigned int)time(0)); //Set a new seed
cout << "Three random integer from 0 to " << RAND_MAX <<": "
<< rand() << " " <<rand()<<" " << rand()<<endl;

return 0;

}这样子的时候运行不成功,
E:\PROGRAM FILES\LEMOLIANXI\C++入门\Random\random integers.cpp(7) : error C2039: 'rand' : is not a member of 'std'
E:\PROGRAM FILES\LEMOLIANXI\C++入门\Random\random integers.cpp(7) : error C2873: 'rand' : symbol cannot be used in a using-declaration
E:\PROGRAM FILES\LEMOLIANXI\C++入门\Random\random integers.cpp(8) : error C2039: 'srand' : is not a member of 'std'
E:\PROGRAM FILES\LEMOLIANXI\C++入门\Random\random integers.cpp(8) : error C2873: 'srand' : symbol cannot be used in a using-declaration
E:\PROGRAM FILES\LEMOLIANXI\C++入门\Random\random integers.cpp(9) : error C2039: 'time' : is not a member of 'std'
E:\PROGRAM FILES\LEMOLIANXI\C++入门\Random\random integers.cpp(9) : error C2873: 'time' : symbol cannot be used in a using-declaration
执行 cl.exe 时出错.
去掉下面这个就可以了
using std::rand;
using std::srand;
using std::time;
把这个内容去掉就可以运行,而且达到取随机数的效果

为什么用了using后就不可以了呢`?
...全文
189 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
wangguangjia85 2009-10-29
  • 打赏
  • 举报
回复
很是感谢4#及各楼的回答
worldli90 2009-07-14
  • 打赏
  • 举报
回复
太感谢了,我都忙了半天了

hu_vane 2008-02-11
  • 打赏
  • 举报
回复
4楼正解。
rand、srand、time不属于std的namespace中。
PS:尽量少用using。
zhangwanlong 2008-02-11
  • 打赏
  • 举报
回复
不用using。。。。
angelcm51 2008-02-10
  • 打赏
  • 举报
回复
是全局的话,就不需要再用using去声明了吧!

sqrt也是全局的么,最近用的东西怎么都会有这些问题

using反而成了多余的事了
ruyunlong2832 2008-02-09
  • 打赏
  • 举报
回复
对,应该也可以写#include <cstdlib>的
arong1234 2008-02-09
  • 打赏
  • 举报
回复
楼主已经#include <cstdlib>了,因此应该是不行的
而且这种using语法在vc6中是不是也不支持啊?
0黄瓜0 2008-02-09
  • 打赏
  • 举报
回复
在VC6中,rand和srand及time没有加入名称空间std中,而是全局的.


#include <iostream>
#include <cstdlib>
#include <ctime>

using std::cout;
using std::endl;
//using std::rand;
//using std::srand;
//using std::time;

int main() {
const int limit1 = 500; //Upper limit for on set of random values
const int limit2 = 31; //Upper limit for another set of values

cout << "First we will use the default sequence from rand(). \n ";
cout << "Three random integer from 0 to " << RAND_MAX << ": "
<< rand() << " " << rand() << " " <<rand() << endl;
cout << endl << "Now we will use a new seed for rand().\n ";
srand((unsigned int)time(0)); //Set a new seed
cout << "Three random integer from 0 to " << RAND_MAX <<": "
<< rand() << " " <<rand() <<" " << rand() <<endl;

return 0;

}
cnzdgs 2008-02-08
  • 打赏
  • 举报
回复
#include <stdlib.h>

64,642

社区成员

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

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