簡體   English   中英

拋出異常:讀取訪問沖突**this**

[英]Exception thrown: read access violation **this**

我試圖通過包含傳遞給函數的對象的兩個單獨的 arrays 進行排序,以找到最高薪水。 我以為我已經弄清楚了,但后來我得到了下面給出的錯誤:“拋出異常:讀取訪問沖突。是 0x12963854。” 我覺得這可能是一個簡單的修復,我只是忽略了它。 這都是使用最新版本的視覺工作室在 c++ 中編寫的。

您錯誤地使用薪水highest和其他最高作為索引otherHighest baseballArray陣列和basketballArray陣列!

您的代碼中有 3 個主要錯誤,按出現順序排列:

  • highestotherHighest未初始化
  • 正如 Kim Nyholm 所指出的,工資被用作指標
  • 然后highestotherHighest最高被用作薪水而不是索引
// Initialized as index of the first element
int highest = 0;
int otherHighest = 0;

for (int i = 0; i < SIZE; i++) {
    if (baseballArray[highest].getSalary() < baseballArray[i].getSalary())
        highest = i; // Stores the index

    if (basketballArray[otherHighest].getSalary() < basketballArray[i].getSalary())
         otherHighest = i; // Stores the index
}

// Compares the salaries
if (baseballArray[highest].getSalary() > basketballArray[otherHighest].getSalary())
    baseballArray[highest].printStats();
else if (basketballArray[otherHighest].getSalary() > baseballArray[highest].getSalary())
    basketballArray[otherHighest].printStats();
else // if (baseballArray[highest].getSalary() == basketballArray[otherHighest].getSalary())
    cout << "error";

暫無
暫無

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

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