gpt4 book ai didi

c++ - 读取位置访问冲突 C++

转载 作者:行者123 更新时间:2023-12-02 03:29:12 25 4
gpt4 key购买 nike

我正在编写一个程序,打印用户输入的号码的完整英文名称。这不是一个完整的程序,但我不断收到错误:

编程挑战 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

这是主程序,我只是使用构造函数为 number 赋值,以使调试更快一点

#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 的数组的有效索引为 0N-1

我建议使用std::vector<std::string> .

关于c++ - 读取位置访问冲突 C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18648440/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com