dll中未处理的异常

dll中未处理的异常

一切似乎都还不错,但是当我输入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/

10-10 18:24
查看更多