[英]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
這是主程序,我只是使用構造函數為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 ++
我不能回答任何問題,因為這真的不是太有條理
one
容納18個元素,但是您可以在其中放置19個元素。
const int SIZE1 = 18;
SIZE1
數組的有效數組索引為0到17。通常,大小為N的數組的有效索引為0到N-1 。
我建議使用std::vector<std::string>
。
這里有兩件事:
您根本沒有調用“ initializeArray()”。 因此,當您嘗試訪問陣列時,那里什么也沒有。 我建議在構造函數中調用它。 像這樣:
Numbers(int num)
{
number = num;
hundred = "hundred";
thousand = "thousand";
one = new string[SIZE1];
ten = new string[SIZE2];
initializeArray();
}
第二,上面的家伙說的是。 您正試圖為大小為18的數組分配19個值,因此數組的大小值不正確。請務必確保使大小大於我們的預期,然后可以進行調整:
const int SIZE1 = 20;
const int SIZE2 = 20;
另外,請參見您的define()? 而不是使用for循環,為什么不呢?
string name = one[number];
編輯:哇,我還錯過了另一件事...您已經兩次聲明了數組指針變量,因此它實際上超出了范圍,以為您想創建一些本地版本。 再次在上方查看我對構造函數的調整后實現。 看看我如何從變量名前面刪除“ String *”。
變量“一”和“十”已從字符串指針更改為包含字符串的向量。 在構造函數中調用了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;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.