2016-04-24 44 views
-2

我一直在這裏坐了幾個小時試圖弄清楚這一點。我一直無法找到類似的問題(儘管我確信它已經完成)。所以在我的問題。當我編譯這個時,它很好。
我應該補充說unsortedList是Book *,它是一個結構體。在寫入對象時運行時拋出異常

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)拋出:0000005:訪問衝突寫入位置00000000。

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 
{ 
    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); 
} 

請幫助歐比旺。你是我唯一的希望...

+0

你的變量'unsortedArrayLength'是一個常量嗎?如果不是,你可能會遇到數組被正確初始化的問題,實際上,我也不確定爲什麼你會使用數組長度變量作爲索引,這會使你返回1個索引超過數組的末尾,這是一個經典的「一個一個」的錯誤,你可能需要發佈更多關於你設計的方式的信息你的數組和索引它的方式 – ciphermagi

+0

我只是要編輯unsortedList的類型爲Book *。我的索引== 1.在另一個函數中,前一個操作的索引處有一個對象 – yorTsenoJ

+0

請包括struct Book的定義 – ciphermagi

回答

0

好吧,我想通了。我讓數組的實例更大。看來我正在觸及數組的上限。數組初始化爲2,我雖然會足夠,但事實並非如此。 (謝謝大家借我所知)

+0

爲什麼不加入21世紀,而是使用一個'std :: vector',它可以根據需要實際增長,而不是隨機猜測一個合適的數組大小? –

+0

我們還沒有在課堂上的向量。我們目前正在製作排序清單。我的教授實際上提到,如果你填充一個數組,你可以編程創建另一個數組,但他沒有詳細說明如何做到這一點。隨着任務的到期日期臨近,我沒有看着它。 :/ – yorTsenoJ

+0

似乎很奇怪,先教會你錯誤的方式,但沒關係。 –

相關問題