[英]Exception thrown: read access violation **this**
我試圖通過包含傳遞給函數的對象的兩個單獨的 arrays 進行排序,以找到最高薪水。 我以為我已經弄清楚了,但后來我得到了下面給出的錯誤:“拋出異常:讀取訪問沖突。這是 0x12963854。” 我覺得這可能是一個簡單的修復,我只是忽略了它。 這都是使用最新版本的視覺工作室在 c++ 中編寫的。
您錯誤地使用薪水highest
和其他最高作為索引otherHighest
baseballArray
陣列和basketballArray
陣列!
您的代碼中有 3 個主要錯誤,按出現順序排列:
highest
和otherHighest
未初始化highest
和otherHighest
最高被用作薪水而不是索引// 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.