访问冲突读取位置 C++
access violation reading location c++
我正在编写一个程序,可以打印用户输入的数字的完整英文名称。 这不是一个完整的程序,但我不断收到错误:
编程挑战 14.1.exe 中0x00b02c76的首次机会异常:0xC0000005:访问冲突读取位置0xcccccd80。编程挑战 14.1.exe 中0x00b02c76时未处理的异常: 0xC0000005:访问冲突读取位置0xcccccd80。
我试着环顾四周,找不到任何对我有用的东西。 这是程序:
头文件:
#ifndef NUMBERS_H
#define NUMBERS_H
#include <string>
using namespace std;
const int SIZE1 = 18;
const int SIZE2 = 8;
class Numbers
{
private:
int number;
string hundred;
string thousand;
string * one;
string * ten;
public:
Numbers(int num)
{
number = num;
hundred = "hundred";
thousand = "thousand";
string * one = new string[SIZE1];
string * ten = new string[SIZE2];
}
void initializeArray()
{
// Intialize array "one"
one[0] = "zero";
one[1] = "one";
one[2] = "two";
one[3] = "three";
one[4] = "four";
one[5] = "five";
one[6] = "six";
one[7] = "seven";
one[8] = "eight";
one[9] = "nine";
one[10] = "eleven";
one[11] = "twelve";
one[12] = "thirteen";
one[13] = "fourteen";
one[14] = "fifteen";
one[15] = "sixteen";
one[16] = "seventeen";
one[17] = "eighteen";
one[18] = "nineteen";
// Initialize the ten array
ten[0] = "ten";
ten[1] = "twenty";
ten[2] = "thirty";
ten[3] = "forty";
ten[4] = "fifty";
ten[5] = "sixty";
ten[6] = "seventy";
ten[7] = "eighty";
ten[8] = "ninety";
}
string determine()
{
string name = "";
for (int i = 0; i <= number; i++)
{
if (number == i)
{
name = one[i];
}
}
return name;
}
~Numbers()
{
delete [] one;
delete [] ten;
}
};
#endif
这是主程序,IM只是使用构造函数为数字赋值以使调试速度更快
#include <iostream>
#include "Numbers.h"
using namespace std;
int main()
{
Numbers n(5);
string name = n.determine();
cout << "The number is " << name << endl;
cin.ignore();
cin.get();
return 0;
}
顺便说一下,这是编译器的VC ++
不回答任何问题,因为这真的不是太有条理
const int SIZE1 = 18;
SIZE1
数组的有效数组索引为 0 到 17。通常,大小为 N 的数组的有效索引为 0 到 N-1。
我建议使用 std::vector<std::string>
.
one
包含 18 个元素,但你在其中放置了 19 个元素。
这里有两件事:
你根本没有调用"initializeArray()"。因此,当您尝试访问阵列时,那里没有任何内容。我建议在构造函数中调用它。喜欢这个:
Numbers(int num)
{
number = num;
hundred = "hundred";
thousand = "thousand";
one = new string[SIZE1];
ten = new string[SIZE2];
initializeArray();
}
第二,是上面人说的。数组大小的值不正确,因为您尝试将 19 个值分配给大小为 18 的数组。只是为了真正确定,让我们使尺寸比我们预期的大,您可以稍后进行调整:
const int SIZE1 = 20;
const int SIZE2 = 20;
另外,请参阅您的 determine()? 而不是使用 for 循环,为什么不去:
string name = one[number];
编辑:哇,我错过了另一件事...您已经声明了两次数组指针变量,因此它实际上超出了范围,认为您想制作一些本地版本。再次查看我上面调整后的构造函数实现。看看我是如何从变量名称之前删除"字符串*"的。
变量"one"和"ten"已从字符串指针更改为保存字符串的向量。在构造函数中调用 initializeArray。更改了为名称字符串分配新字符串的方式。这是工作代码。
class Numbers
{
private:
int number;
string hundred;
string thousand;
vector<string> one;
vector<string> ten;
public:
Numbers(int num)
{
number = num;
hundred = "hundred";
thousand = "thousand";
initializeArray();
}
void initializeArray()
{
one.push_back("zero");
one.push_back("one");
one.push_back( "two");
one.push_back("three");
one.push_back("four");
one.push_back("five");
one.push_back("six");
one.push_back("seven");
one.push_back("eight");
one.push_back("nine");
one.push_back("eleven");
one.push_back("twelve");
one.push_back("thirteen");
one.push_back("fourteen");
one.push_back("fifteen");
one.push_back("sixteen");
one.push_back("seventeen");
one.push_back("eighteen");
one.push_back("nineteen");
// Initialize the ten array
ten.push_back("ten");
ten.push_back("twenty");
ten.push_back("thirty");
ten.push_back("forty");
ten.push_back("fifty");
ten.push_back("sixty");
ten.push_back("seventy");
ten.push_back("eighty");
ten.push_back("ninety");
}
string determine()
{
string name("");
for (int i = 0; i <= number; i++)
{
if (number == i)
{
auto iter = one.begin();
iter += i;
name.assign(*iter);
}
}
return name;
}
~Numbers()
{
}
};
int main()
{
Numbers n(5);
string name = n.determine();
cout << "The number is " << name << endl;
cin.ignore();
cin.get();
return 0;
}
- 将值指定给向量(2D)的向量中的某个位置
- 使用Unreal C++获取VR耳机的世界位置/方向
- 写入位置0x0000000C时发生访问冲突
- 如何将两个不同矢量的同一位置的两个元素组合在一起
- GL_SHADERSTORAGE_BUFFER位置是否与其他着色器位置冲突
- 如何使用cudaMallocManaged在指针位置初始化对象?(C++)
- 无法获取webot::PositionSensor对象中位置传感器的值
- 非常量变量只读位置的赋值
- 如何定义更改car类中car位置的方法
- 使用迭代器时如何访问对象在向量中的位置?
- 更改.cpp程序的输入文件中数据的位置会意外更改输出
- 求最大元素位置的分治算法
- 你好。。。id_public变量不应该给出结果为 81 和 86 吗?为什么它为两个派生类占用不同的内存位置?
- 为什么我的点没有在 OpenGL 中绘制鼠标所在的位置?
- 如何将元素添加到向量的字符串位置
- 如何选择在 csv 文件中输出的位置
- 无法打开 fstream C++文件,即使它与 .cpp 位于同一位置
- 如何在特定位置追加 json 文件
- 我的目标是编写一个程序来计算和存储字符串在字符数组中出现的位置
- OpenGL - 在 NDC 中计算位置适用于着色器,但不适用于'regular'程序