【发布时间】:2014-06-07 05:01:05
【问题描述】:
我正在尝试编写的程序是创建两个对象来分别显示 1000-9000 和 100-900 之间的随机数。
我正在尝试编写我的第一个使用类和多个文件但有问题的 C++ 程序。我在 main() 函数的 for 循环中遇到错误;它在“R”之前表示预期的主表达式,第二个 for 循环也表示“
感谢任何帮助或指导:)
main.cpp:
#include <iostream>
#include "Random.h"
#include <cstdlib>
#include <ctime>
using namespace std;
int main()
{
srand(time(0));
for(int i = 0; i < 10; ++i)
{
cout << RandomNum four(1000,9000); << endl;
}
for(int i = 0; i < 10; ++i)
{
cout << RandomNum three(100,900); << endl;
}
}
随机.h:
#ifndef RANDOM_H
#define RANDOM_H
class RandomNum
{
public:
RandomNum(int ix, int iy);
int operator ()();
int operator ()(int ny);
int operator ()(int nx, int ny);
int x,y;
};
#endif
randomNum.cpp:
#include <iostream>
#include "randomInt.h"
#include <cstdlib>
using namespace std;
RandomInt::RandomInt(int ix, int iy):x(ix), y(iy)
{}
int RandomInt::operator()()
{
return x + rand() % (y - x + 1);
}
int RandomInt::operator()(int ny)
{
return x + rand() % (ny - x + 1);
}
int RandomInt::operator()(int nx, int ny)
{
return nx + rand() % (ny - nx + 1);
}
【问题讨论】:
-
语法错误与标题中的任何内容无关。再读一遍(仔细)并修复它。
RandomNum four(1000,9000)应该做什么?在什么上下文? -
感谢您的意见这样更有意义吗?
标签: c++ class random numbers generator