2013-06-02 22 views
0

我對C++相當陌生,因爲這是我所接受的第一個類。我正在創建一個類,它將根據執行的操作動態創建/刪除一個數組。當我嘗試向數組中添加某些東西時我收到了這個異常,我相信。任何幫助深表感謝。編碼動態數組時的內存異常C++

代碼:

#include "ArrayList.h" 
#include < string> 

using namespace std; 

int count = 0; 

ArrayList::ArrayList(){ 
Object *items = new Object[count]; 
items[0] = "NULL"; 
}  

ArrayList::~ArrayList(){ 
releaseList(); 
} 

void ArrayList::releaseList(){ 
delete[] items; 
count = 0; 
ArrayList(); 
} 

void ArrayList::add(Object o){ 
Object *temp = new Object[count + 1]; 
for(int i = 0; i < count; i ++){ 
    temp[i] = items[i]; 
} 
temp[count + 1] = o; 
delete[] items; 
items = temp; 
delete[] temp; 
count = count + 1; 
} 

void ArrayList::add(Object o, int index){ 
Object *temp = new Object[count + 1]; 
for(int i = 0; i < index; i++){ 
    temp[i] = items[i]; 
} 
temp[index] = o; 
for(int i = index + 1; i < count -1; i ++){ 
    temp[i] = items[i]; 
} 
delete[] items; 
items = temp; 
delete[] temp; 
count = count + 1; 
} 

爲ArrayList的

#ifndef ARRAY_LIST_H 
#define ARRAY_LIST_H 

#include <string> 
using namespace std; 

typedef string Object; 


class ArrayList { 
private: 
Object *items;  // a dynamic array of pointers to Objects 
int numberOfItems; 

// Releases all the memory allocated to the list; resets the object to its 
// default values. 
void releaseList(); 

public: 
// Initializes object to default values (NULL, 0). 
ArrayList(); 

// Destroys the object. Calls releaseList() to do the actual work. 
~ArrayList(); 

// Appends the object o to the end of the list. This resizes the array by 1. 
void add(Object o); 

// Adds the object o and the specified index. This resizes the array by 1. 
void add(Object o, int index); 

// Removes all the items from the list. Resets object to its default state. 
void clear(); 

// Returns true if the list contains the object o. 
bool contains(Object o) const; 

// Returns the object at the specified index. Assumes index is in range. 
Object objectAt(int index) const; 

// Returns the index of the first matching object o. Assumes the object is in the list. 
int indexOf(Object o) const; 

// Returns the index of the last matching object. Assumes the object is in the   list. 
int lastIndexOf(Object o) const; 

// Returns true if list is empty. 
bool isEmpty() const; 

// Removes the object o from the list. List is resized. Assumes object is present. 
void remove(Object o); 

// Removes the object o at the specified index. List is resized. Assumes index is in range. 
void remove(int index); 

// Returns the number of elements in the list. 
int size() const; 

// Sets the element at the specified index. 
void set(Object o, int index); 

// Returns a string containing all the items comma separated and surrounded 
// by curly ({) braces, i.e. {item1, item2, ...} 
string toString(); 
}; 

#endif 
+0

hw01.exe中的0x0fb1ca58(msvcr100d.dll)的第一次機會異常:0xC0000005:訪問衝突寫入位置0xabababab。 hw01.exe中的0x775c15de未處理的異常:0xC0000005:寫入位置0xabababab的訪問衝突。 – Nealio

+0

'items [0] =「NULL」;'?你的意思是'項目[0] = NULL'吧? –

+0

是的,那是早些時候給我的例外,我沒有改變它。 – Nealio

回答

3

這是一個問題,在ArrayList::add .h文件,這將引發訪問衝突異常:

Object *temp = new Object[count + 1]; 
... 
temp[count + 1] = o; 

你'將對象分配給數組末尾的內存位置。 C++的索引從零開始,這意味着如果你有

int A = new int[5]; 

那麼有效的位置是A[0] ... A[4],但不A[5]

+1

這幫助我解決了這個問題的部分,謝謝:) – Nealio