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

angelcm51 angelcm51 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
#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后就不可以了呢`?
...全文
评论 9
收藏 已收藏
分享
返回顶部
angelcm51
C++ 语言 65,214 成员 250,514 内容