【问题标题】:access violation reading location c++访问冲突读取位置c ++
【发布时间】:2013-09-06 01:24:51
【问题描述】:

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

编程挑战 14.1.exe 中 0x00b02c76 处的第一次机会异常:0xC0000005:访问冲突读取位置 0xcccccd80。 Programming Challenge 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

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

#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++

我会回答任何问题,因为这不是太有条理

【问题讨论】:

  • 为什么不使用字符串向量?

标签: c++


【解决方案1】:
const int SIZE1 = 18;

SIZE1 数组的有效数组索引为 0 到 17。通常,大小为 N 的数组的有效索引为 0N -1

我推荐使用std::vector&lt;std::string&gt;

【讨论】:

    【解决方案2】:

    one 包含 18 个元素,但您在其中放置了 19 个元素。

    【讨论】:

    • 我不敢相信我错过了。非常感谢你们两个,看看它是否有效
    【解决方案3】:

    这里有两件事:

    您根本没有调用“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;
    

    另外,看到你的确定()?而不是使用 for 循环,你为什么不去:

    string name = one[number];
    

    编辑:哇,我还错过了另一件事……您已经两次声明了数组指针变量,因此它实际上超出了范围,认为您想要制作一些本地版本。再看看我上面的构造函数的调整实现。看看我是如何从变量名之前删除“String *”的。

    【讨论】:

    • 我提出了你的建议,但它仍然向我抛出错误,想看看编辑后的代码吗?也许我错过了什么?
    • 嗯,让我编辑我的答案以确保我们都在同一页面上。
    • 好的,你在构造函数中告诉我的,至于不包含 if/else 语句的赋值,对吗?我不断收到错误消息,您认为这可能是我错误实施的析构函数吗?
    • 其实我明白你的意思是修改确定。
    • 我还发现了另一个问题...在我的编辑中注明。刚刚将它加载到我的编译器中并且它工作了:)
    【解决方案4】:

    变量“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;
    }
    

    【讨论】:

    • 哦哇我对向量不太熟悉,我只知道它们的基本功能和用途但我不熟悉
    • auto iter = one.begin();迭代+=我; name.assign(*iter);
    猜你喜欢
    • 2020-03-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-12
    相关资源
    最近更新 更多