写入对象时,运行时抛出异常错误

时间:2016-04-24 01:04:29

标签: c++ visual-c++-runtime

我一直坐在这里几个小时试图解决这个问题。我一直无法找到类似的问题(虽然我确信它已经完成)。所以关于我的问题。当我编译它时,它很好。
  我应该补充一点,unsortedList是Book *,它是一个struct。

string tit = tempBook->title;
string act = tempBook->action;
string aut = tempBook->author;
string sub = tempBook->subject;
char* tm = tempBook->time;

unsortedList[unsortedArrayLength].title;
unsortedList[unsortedArrayLength].action;
unsortedList[unsortedArrayLength].author;
unsortedList[unsortedArrayLength].subject;
unsortedList[unsortedArrayLength].time;  

然而,当我编译它时,我收到一个错误:
在Assign.exe中的0x5AC6516F(vcruntime140d.dll)抛出异常:0xC0000005:访问冲突写入位置0x00000000。

string tit = tempBook->title;
string act = tempBook->action;
string aut = tempBook->author;
string sub = tempBook->subject;
char* tm = tempBook->time;

unsortedList[unsortedArrayLength].title = tit;
unsortedList[unsortedArrayLength].action = act;
unsortedList[unsortedArrayLength].author = aut;
unsortedList[unsortedArrayLength].subject = sub;
unsortedList[unsortedArrayLength].time = tm;  

然后它会弹出窗口memcpy.asm,光标位于此位置:

CopyUpByteLoop:
    mov     al, byte ptr [esi]
    mov     byte ptr [edi], al
    inc     esi
    inc     edi
    dec     ecx
    jne     CopyUpByteLoop  

根据要求定义struct Book:

    struct Book
{
    std::string title;
    std::string author;
    std::string subject;
    char* time;
    std::string action;
};

这是完整的功能:

    void DB::insertBook(Book* tempBook)
{
    using namespace std;
    unsortedArrayLength++;
    string tit = tempBook->title;
    string act = tempBook->action;
    string aut = tempBook->author;
    string sub = tempBook->subject;
    char* tm = tempBook->time;

    unsortedList[unsortedArrayLength].title = tit;
    unsortedList[unsortedArrayLength].action = act;
    unsortedList[unsortedArrayLength].author = aut;
    unsortedList[unsortedArrayLength].subject = sub;
    unsortedList[unsortedArrayLength].time = tm;

    system("cls");

    cout << "You have " << unsortedList[unsortedArrayLength].action <<":\n" << endl <<
    unsortedList[unsortedArrayLength].title << endl <<
    unsortedList[unsortedArrayLength].author << endl <<
    unsortedList[unsortedArrayLength].subject << endl <<
    "on " << unsortedList[unsortedArrayLength].time << endl << endl;

    printToLog(tempBook);
}

请帮助欧比旺。你是我唯一的希望......

1 个答案:

答案 0 :(得分:0)

好的,我明白了。我使数组的实例化更大。看来我正在击中阵列的上界。数组初始化为2,虽然我已经足够了,但事实并非如此。 :(谢谢大家借给我他们的知识。