2016-04-24 46 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

似乎很奇怪,先教会你错误的方式,但没关系。 –

相关问题