我已經創建了一個方法,應該刪除數組的第一個元素,但是當我運行我的代碼時,調試器翻轉出來,我不知道爲什麼。數組,removeFirst()不工作?
這是我的removeFirst()方法:
Loan & ListOfLoans :: removeFirst(){
index = 0;
//determine if the container needs to be shrunk
if((numberOfElements < capacity/4) && (capacity >= 4)){ // shrink container when array is 1/4 full
cout<<"shrinking array! \n";
Loan ** temp = elements;
elements = new Loan * [numberOfElements/2];
//copy temp array to elements
for(int i = 0; i<numberOfElements; i++){
temp[i] = elements[i];
numberOfElements = numberOfElements/2;
delete [] temp;
}
}
numberOfElements--;
return **elements;
}
而且好措施我的頭文件:
#include <iostream>
#include "loan.h"
using namespace std;
class ListOfLoans {
public:
ListOfLoans(int initial_size=4);
~ListOfLoans(void);
void add(Loan & aLoan);
Loan & first() ;
Loan & removeFirst();
// answer the first element from the list and remove it from the list
// if the resulting list is more than three quarters empty release some memory
Loan & removeLast();
// answer the last element from the list and remove it from the list
// if the resulting list is more than three quarters empty release some memory
Loan & next();
int size();
private:
Loan ** elements; //actuall stuff in the array m_pnData;
int numberOfElements; //number of elements in the list stize of the array? m_nLength
int capacity; //size of the available array memory
int index; //used to help with the iteration
};
我真的很陌生,所以請耐心等待我...我無法使用貸款聲明to_return,因爲沒有適當的默認構造函數...我認爲我的構造函數要求我發起每個新貸款5參數,我不能有兩個構造函數,因爲我已經給了頭文件並且不能改變它 – Sarah
在這種情況下調查'boost :: ptr_deque'或'boost :: ptr_list'。這些是標準容器的包裝,允許他們使用指針。 –