所以我的問題是:我想知道如何刪除(帳戶指針數組),以便我可以用我的dummyA數組的內容重新填充數組。我試着delete[]
和delete accounts[j]
與for循環,但無濟於事我不斷收到錯誤:_ASSERTE(_BLOCK_TYPE_IS_VALID(pHead->nBlockUse));
老實說,我不知道這是什麼意思.. 經研究,我最好的猜測是,我的問題是,我刪除我的數組不止一次,但我不知道如何..實質上,我指向新對象的帳戶數組在數組中間有一個不相關的對象,所以我僞造了一個虛擬對象,將所有相關對象放入到清除的目標中賬戶中的所有對象然後用虛擬內部的對象替換對象,我想節省內存並嘗試高效。
//頭文件
IFNDEF H_ChartOfAccounts
限定H_ChartOfAccounts
包括串
使用命名空間std;指向在類中聲明的對象的指針數組?
const int size = 100;
class cOfA
{
protected:
int actNumber;
string actName;
int actType;
static cOfA* accounts[size];<Dynamic Array of Pointers to Object "In Question"
static int totalAccounts;
public:
cOfA();
virtual ~cOfA();
void getAccount();
static void createAccount();
static void addAccount();
static void delAccount();
static void displayAccount();
void saveAccount();
};
**endif**
//>Implementation File For Above Header File
void cOfA :: delAccount()
{
int searchItem;
char ch;
cout<<"\nEnter the Account Number that you want to delete: ";
cin>>searchItem;
for (int j = 0; j < totalAccounts; j++)
{
if (accounts[j]->actNumber == searchItem)
{
cout<<"\nDo you want to delete this account: \n";
cout<< accounts[j]->actName<<"/";
cout<< accounts[j]->actNumber<<"/";
cout<< accounts[j]->actType<<endl;
cin>>ch;
if(ch == 'y')
{
accounts[j]->actName = " ";
accounts[j]->actNumber = 0;
accounts[j]->actType = 0;
totalAccounts--;
break;
}
else
{
cout<<"\nEnter the Account Number you want to delete: ";
cin>>searchItem;
}
}//end if
}// for loop
cOfA* dummyA[size];
int l = 0;
for (int k = 0;k < totalAccounts+1; k++)
{
if(accounts[k]->actNumber != 0)
{
dummyA[l] = new cOfA;
*dummyA[l] = *accounts[k];
l++;
}
}//end for loop
delete[] accounts;//<--"When I compile, I get My Error Here"
//Break Statement:" _ASSERTE(_BLOCK_TYPE_IS_VALID(pHead->nBlockUse)); "
int j = 0;
while (j < totalAccounts)
{
accounts[j] = new cOfA;
*accounts[j] = *dummyA[j];
j++;
}//end while loop
delete[] dummyA;
}
首先,修理您的縮進。其次,製作一個[testcase](http://sscce.org)。 –
您可以編輯您的帖子,而不是在評論中編寫代碼段。 –
第三次:代碼截斷 –