簡體   English   中英

拋出異常:讀取訪問沖突。 _Pnext 是 0x148F854。 (C++ 析構函數錯誤)

[英]Exception thrown: read access violation. _Pnext was 0x148F854. (C++ Destructor Error)

case 4: //  ----------------------------------------    Delete Customer
                cout << "Enter your Customer ID:" << endl;
                cin >> exist_id;
                for (int i = 0; i <= cnum; i++) {
                    if (c[i].C_ID() == exist_id) {
                        cc = 't'; // check to set customer found.

                        c[i].~customer(); // error occurs here

                        c[i].id(NULL);
                        cout << "Deletion Successful" << endl << endl;
                        break;
                    }
                };

                if (cont(cc) == false) {
                    cout << "CUSTOMER NOT FOUND" << endl << endl;
                }
                cc = 'f';
                //system("CLS");
                break;

在上述情況下,我拋出了異常:讀取訪問沖突。 _Pnext 是 0x148F854 錯誤。

該代碼在 2020 年 2 月 3 日完美運行。 但是今天從數組中刪除客戶時會出現此錯誤。

Visual Studio 錯誤警報

C++ 中的析構函數在 object 死亡/銷毀以處理不需要的分配 memory 首先被調用,它不應該在其他任何地方調用。

在您的情況下,我認為最好的做法是在customer class 中創建一個成員 function delete_customer ,該成員在析構函數中有確切的代碼。

或者,為了不重復代碼兩次,您可以在 object 死亡時調用的析構函數中調用delete_customer function。

希望這能解決您的問題!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM