c++ - msvcr110d.dll 中未处理的异常

标签 c++ exception unhandled

一切似乎都很好,但是当我输入 I 时,它说

Unhandled exception at 0x64A1EB90 (msvcr110d.dll) in ConsoleGame1.exe: 0xC0000005: Access violation writing location 0xCCCCCCCC.

First-chance exception at 0x64A1EB90 (msvcr110d.dll) in ConsoleGame1.exe: 0xC0000005:     Access violation writing location 0xCCCCCCCC.
Unhandled exception at 0x64A1EB90 (msvcr110d.dll) in ConsoleGame1.exe: 0xC0000005: Access violation writing location 0xCCCCCCCC.
The program '[5088] ConsoleGame1.exe' has exited with code 0 (0x0).

代码:

void Inventory();

struct Item
{
    string itemName;
    string itemDescription;
    int itemNumber;
    bool haveItem;

    void DisplayItem();
};

int main()
{
    char inv;
hint:
    cout << "HINT: To open your inventory  press 'I'.\n";
    cin >> inv;
    if (inv=='I') Inventory();
    else goto hint;
    system("pause");
    return 0;
}

void Inventory()
{
    Item Letter =
    {
        Letter.itemName = "Letter",
        Letter.itemDescription = "...",
        Letter.itemNumber = 1,
        Letter.haveItem = true
    };
    Item Wood =
    {
        Wood.itemName = "Wood",
        Wood.itemDescription = "Birch wood.",
        Wood.itemNumber = 2,
        Wood.haveItem = false
    };
    Letter.DisplayItem();
    Wood.DisplayItem();
}

最佳答案

为了解决手头的问题,您要分配给尚未构造的对象:

Item Letter =
{
    Letter.itemName = "Letter",
    Letter.itemDescription = "...",
    Letter.itemNumber = 1,
    Letter.haveItem = true
};

在为 initialisng Letter 指定参数时,您正在分配给 Letter 的成员。那不行。你所追求的是:

Item Letter =
{
    "Letter",
    "...",
    1,
    true
};

但是,代码通常显示您最好从基础开始,使用 good book指导你,正如我在评论中所说。例如,您肯定不想使用goto 而不是循环。 Item 类可以使用构造函数。

关于c++ - msvcr110d.dll 中未处理的异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17949701/

相关文章:

c++ - 由于类型为QLineSeries,所以单击了QAbstractSeries信号

c++ - 在 C++/FORTRAN 互操作中处理字符串的官方方法是什么

c++ - Eclipse 上的软件自动生成文档

Java无效的流头问题

c# - 出现错误 “value ' 0' is not a valid value”

java - 如何测试未处理的消息

c++ - libcurl:检测 block 编码响应的 block 边界

Python:如何区分套接字错误和超时?

logging - Enterprise Library 5.0 - 将自定义标记添加到 TextFormatter

c++ - 未处理的异常 - OpenCV - cvReleaseCapture 和 cvReleaseImage - C++