矢量:内存范围错误
Posted
技术标签:
【中文标题】矢量:内存范围错误【英文标题】:Vector: range error at memory 【发布时间】:2018-04-02 13:36:47 【问题描述】:之前,我一直在 CodeBloks 中使用 C++ 进行一些编程,从一段时间以来,我开始在 MS Studio 中进行编程。目前我正在编写书中的一个小游戏:编程:使用 C++ 的原则和实践。第 5 章的练习 12。 我一直在互联网上四处寻找,以修复无论我尝试什么都会不断发生的范围内存错误(请参阅打印屏幕链接)。 关于程序的描述在代码本身中。 我还确实将向量设置为硬编码(如果我说得对的话)大小,以使程序不再抱怨它们的内存范围错误。
注意:是的,这也是一个学校练习/练习。但是我宁愿问我做错了什么,然后从互联网上复制粘贴代码..
请查看我的代码并查看,否则我在向量方面犯了一些错误,因为这可能是我的错误的来源。
更新:作为本主题的答案。 Link to update
错误:
Unhandled exception at 0x7529CBC2 in Tweede project.exe: Microsoft C++ exception: Range_error at memory location 0x006FF510.
程序:
#include "std_lib_facilities.h"
int main()
bool while_bit = false;
vector<int>bulls_cows(4);
vector<int>inputs_arr(4);
//Generate 4 random numbers.
for (int a = 0; a < 4; ++a)
bulls_cows[a] = a + 1;//randint(a) % 9 + 0; //random number between 0 and 9.
//bulls_cows[1] = randint(10);
cout << "For this game, called Bulls and Cows, you have to guess the four right numbers between 0 and 9.\n";
cout << "When one or more digets are right and in the right position the program will say the number of Bulls.\n";
cout << "When one or more digets are right but not in the right position the program will say the number of Cows.\n";
cout << "Please enter 4 number by filling in ONE positive diget and press enter. Do this four times and wait for the result.\n";
for (int z = 0; z < 4; ++z)
cout << bulls_cows[z] << "\n";
while (while_bit == false)
int input = 0; //Reset of input, cows and bulls every round.
int cows = 0;
int bulls = 0;
cin >> input;
//Test for legit input. If legit then it writes it to the array called "input_arr"
if (input < 0 || input > 9)
cout << "Number must be between 0 and 9.\n";
else
inputs_arr.push_back(input);
//Check or 4 numbers have been given.
if (sizeof(inputs_arr) < 4)
//Check for equal numbers on same spot.
for (int b = 0; b < 4; ++b)
if (inputs_arr[b] == bulls_cows[b])
bulls + 1;
//Check for a number in all spots.
for (int c = 0; c < 4; ++c)
if (inputs_arr[c] == bulls_cows[0] || bulls_cows[1] || bulls_cows[2] || bulls_cows[3])
cows +1;
if (bulls < 4)
cout << "You did not guess the right combination.\n";
cout << "Number of bulls: " << bulls << "\n";
cout << "Number of cows: " << cows << "\n";
inputs_arr[0, 0, 0, 0]; //Empty array.
if (bulls == 4)
cout << "You guessed the right combination!\n";
while_bit = true;
keep_window_open(); //To keep terminal open since MS Studio doesn't itself.
return 0;
【问题讨论】:
请不要链接文字图片。只需输入文本本身,可能标记为引号或代码。std_lib_facilities.h
那是什么?
谷歌 sizeof 运算符的作用
std_lib_facilities.h 是书中的一个库。它有最基本的库,如 将sizeof
运算符与inputs_array
向量一起使用将为您提供该对象在内存中的大小(以字节为单位)。也许你以前用过这个数组。
相反,我认为您想要推入向量中的项目数。 std::vector
有一个名为 size
的函数,它给出推送给它的项目数。
【讨论】:
是的。我确实想要数组中的项目数。我会仔细看看的。谢谢! :) @rem97 如果您正在为向量和数组之间的差异而苦恼,here is a listing of their qualities。 我对此有所了解,但也许我可能会将向量视为数组。会看看它,看看我能学到什么。谢谢! :)【参考方案2】:在 Visual Studio 中,你真的应该,真的将警告级别设置为 4 级。然后编译器会告诉你一些你尝试做的奇怪的事情。
例如:
warning C4552: '+': operator has no effect; expected operator with side-effect 1>
bulls + 1;
您计算结果,但从不将其存储在任何地方。奶牛也一样。
error C4548: expression before comma has no effect; expected expression with side-effect
inputs_arr[0, 0, 0, 0]; //Empty array.
这不是清空数组的方法。要删除所有元素,请执行inputs_arr.clear();
。
warning C4127: conditional expression is constant
if (sizeof(inputs_arr) < 4)
正如我们已经看到的,sizeof
是矢量对象的大小(始终相同),而不是它所包含的元素数量。
然后有一些逻辑错误。
vector<int>inputs_arr(4);
这将创建一个最初包含 4 个整数的向量。
但稍后当你这样做时
inputs_arr.push_back(input);
它将 更多 个元素添加到向量中,因此现在它最多可以容纳 8 个整数。您必须决定是要先创建完整尺寸的,还是要在进行时添加元素。但不是两者兼而有之。
另一个问题是条件
if (inputs_arr[c] == bulls_cows[0] || bulls_cows[1] || bulls_cows[2] || bulls_cows[3])
即使有 种语言可以在其中编写与此类似的条件,但在 C++ 中却不能。要将变量x
与其他几个值进行比较,您必须将其拼写出来:
if (x == y || x == z || x == w)
【讨论】:
感谢您的反馈。我会努力的。我确实不知道您无法将不同的事物与多个 or 语句进行比较。还教导用零重新声明数组将清除其以前的值。从这篇文章中学到了很多:)【参考方案3】:更新:(不确定或者我不得不将其添加到问题中或作为答案。如果有错误,请告诉我。我是堆栈溢出的新手;))
似乎有些语句/循环括号没有正确放置。我编辑了一下: 可悲的是,我的 intergers cows 和 bulls 似乎没有在第 61 行和第 62 行重置。任何人都知道我在那里可能做错了什么。
日志:
cin 没有循环。还编辑了 else-statement 'inputs_arr.pushback(input)' 并在其下方添加了所有 for 循环和语句。修复了部分cin问题。
将公牛、奶牛和输入整数替换到程序顶部,并在 false/if 语句的末尾重置。
将所有循环和语句放在 if-statement '(int b = 0; b
将 for 循环编辑为 >4,其中 couts “数组大小”以查看它具有哪些值。稍后会被删除,因为它仅用于测试。
#include "std_lib_facilities.h"
int main()
bool while_bit = false;
int input = 0;
int cows = 0;
int bulls = 0;
vector<int> bulls_cows(4); //size needed to prevent memory size error at line 11.
vector<int> inputs_arr;
//Generate 4 random numbers. Need to add seed later.
for (int a = 0; a < 4; ++a)
bulls_cows[a] = randint(a) % 9 + 0; //random number between 0 and 9.
cout << "For this game, called Bulls and Cows, you have to guess the four right numbers between 0 and 9.\n";
cout << "When one or more digets are right and in the right position the program will say the number of Bulls.\n";
cout << "When one or more digets are right but not in the right position the program will say the number of Cows.\n";
cout << "Please enter 4 numbers by filling in ONE positive diget and press enter. Do this four times and wait for the result.\n";
for (int z = 0; z < 4; ++z) //Gives the generated numbers for testing.
cout << bulls_cows[z] << "\n";
while (while_bit == false)
cin >> input;
//Test for legit input. If legit then it writes it to the array called "input_arr"
if (input < 0 || input > 9)
cout << "Number must be between 0 and 9.\n";
else
inputs_arr.push_back(input);
//Check or 4 numbers have been given.
if (inputs_arr.size() > 3)
//Check for equal numbers on same spot.
for (int b = 0; b < 4; ++b)
if (inputs_arr[b] == bulls_cows[b])
++bulls;
//Check for a number in all spots.
for (int c = 0; c < 4; ++c)
if (inputs_arr[c] == bulls_cows[0] || inputs_arr[c] == bulls_cows[1] || inputs_arr[c] == bulls_cows[2] || inputs_arr[c] == bulls_cows[3])
++cows;
/*for (int x = 0; x < 4; ++x) //Couts again the fresh entered numbers for a better overview.
cout << "Size of array: " << inputs_arr[x] << "\n";
*/
if (bulls < 4)
cout << "You did not guess the right combination.\n";
cout << "Number of bulls: " << bulls << "\n";
cout << "Number of cows: " << cows << "\n";
int cows = 0; //Reset of cows and bulls each round.
int bulls = 0;
inputs_arr.clear(); //Empty the array.
cout << "Please enter 4 numbers:\n";
if (bulls == 4)
cout << "You guessed the right combination!\n";
while_bit = true;
keep_window_open(); //To keep terminal open since MS Studio doesn't itself.
return 0;
【讨论】:
更新:修复了我重置奶牛和公牛的问题。我声明了它们而不是调用它们。我删除了 int:"cows = 0;"它解决了我的问题。这么小的东西,我找不到。应该是累了^^以上是关于矢量:内存范围错误的主要内容,如果未能解决你的问题,请参考以下文章