0

一切似乎都很好,但是当我进入时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();
}
4

1 回答 1

1

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

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

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

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

但是,正如我在评论中所说,一般来说,代码表明你最好从基础开始,用一本好书来指导你。例如,您绝对不想使用goto循环来代替。该类Item可以使用构造函数。

于 2013-07-30T14:29:55.567 回答