C++:自定义rand_int函数的参数未初始化
C++: Params for custom rand_int function not being initialized
对于内联代码的长时间转储,我提前道歉,但我真的找不到任何不相关的我可以删除的东西,因为我不知道问题到底在哪里。
我正在尝试创建一个以简单方式处理在某个范围内生成随机数的类,但是每当我运行代码时,我都会得到垃圾值,这可能表明某些内容未在某处正确初始化。问题是我找不到哪里。
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
// calculates a random number within a range.
class Random
{
int x, y;
int range;
public:
Random() : x(0), y(1), range(1) {
srand(static_cast<unsigned int>(time(NULL)));
}
~Random() {}
// setters and getters:
void setX(int x) {this->x = x;}
void setY(int y) {this->y = y;}
void setRange(void) {this->range = (this->y - this->x) + 1;}
int getX(void) {return this->x;}
int getY(void) {return this->y;}
int getRange(void) {return this->range;}
// member functions:
// changes negative numbers to positive.
void absXY(void) {
if(getX() < 0) setX(getX() * -1); // changes negative to positive.
if(getY() < 0) setY(getY() * -1);
}
// swaps values around if x > y.
void swapXY(void) {
if(getX() > getY()) { // swaps x with y.
int tmp = getX();
setX(getY());
setY(tmp);
}
}
// calculates rand num and returns it.
int getRandIntFrom(int a, int b) {
setX(a);
setY(b);
setRange();
absXY();
swapXY();
return getX() + (rand() % getRange());
}
};
int main(int argc, char* argv[])
{
Random r;
if(argc == 3) {
cout << r.getRandIntFrom((int)argv[1], (int)argv[2]) << endl;
cout << r.getX() << endl;
cout << r.getY() << endl;
}
else {
cout << "Invalid number of command line arguments." << endl;
return -1;
}
return 0;
}
我一次又一次地查看了这段代码,但看不到问题所在。
创建 Random 的对象时,PRNG 已设定种子,并在初始值设定项列表中为
x
、y
和range
变量提供默认值。二传手和吸盘手是有效的,就我曾经使用过它们而言。
设置和获取是在
getRandIntFrom()
函数中完成的。我使用值 2 和 8 运行程序,从而跳过了
absXY()
和swapXY()
函数中的所有代码,因此这些代码不会搞砸。计算随机数本身的程序逻辑是合理的。
rand() % x
返回从0
到x-1
的任何位置,因此 mod 按 1+范围rand()
并添加下限。使用 2 和 8,这是:rand() % ((8-2)+1) -> rand() % 7 => 0..6 -> 2+(rand() % 7) => 2..8
然而,当我运行程序时,我每次都会得到垃圾结果。
> g++ Random.cpp -o random -Wall -std=c++11
> random 2 8
5641728
5641728
5642296
这里可能出了什么问题?我认为这可能与初始值设定项列表有关,但这是我唯一能想到的。
当您读取命令行参数时,您正在将 c 字符串转换为整数。您得到的是存储命令行参数的地址。因此,您的 Random 类也会返回与此地址 + 给定范围匹配的值。代替强制转换,您可以使用atoi()将主函数更改为。
int main(int argc, char* argv[])
{
Random r;
if (argc == 3) {
cout << r.getRandIntFrom(atoi(argv[1]), atoi(argv[2])) << endl;
cout << r.getX() << endl;
cout << r.getY() << endl;
}
else {
cout << "Invalid number of command line arguments." << endl;
return -1;
}
return 0;
}
编辑:
亚历克斯更快
字符串不能转换为带有强制转换的整数:
r.getRandIntFrom((int)argv[1], (int)argv[2])
改为考虑std::stoi()
或类似
try
{
cout << r.getRandIntFrom(std::stoi(argv[1]), std::stoi(argv[2]) << endl;
cout << r.getX() << endl;
cout << r.getY() << endl;
}
catch (std::invalid_argument&)
{
cout << "invalid argumentn";
return -1;
}
catch (std::out_of_range&)
{
cout << "out of rangen";
return -1;
}
相关文章:
- C++转换参数初始化问题
- 在构造函数中使用可变参数初始化 std::tuple
- 类模板专用化演绎是否应该考虑演绎指南参数初始化?
- 如何通过参数初始化构造函数中的数组?
- 在模板类中使用"this"会导致参数初始化错误
- 如何使用一些参数初始化动态分配的数组中的对象
- 使用模板参数初始化静态 const 成员
- 基于模板参数初始化静态字符
- 使用参数初始化
- 使用参数包参数初始化"std::array"
- 从按值构造函数参数初始化的引用成员
- 使用派生的[C++]生成的参数初始化基类
- 一个C++默认参数可以用另一个参数初始化吗
- 如何用功能指针作为参数初始化静态成员的模板
- 用参数初始化另一类构造函数中的对象
- C :如何通过其构造函数的参数初始化类内部矩阵的尺寸
- C++:基于构造函数参数初始化向量
- 正在使用模板参数初始化stl类
- 如果类 A 修改其构造参数,我可以使用常量参数初始化 const A 吗?
- 使用结构非类型可变参数模板参数初始化结构的字符串数据成员