Kayla Hillis
Kayla Hillis

Reputation: 13

Exception thrown: read access violation **this**

I am trying to sort through two separate arrays containing objects passed to functions in order to find the highest salary. I thought I had it figured out, but then I get the error given below: "Exception thrown: read access violation. this was 0x12963854." I feel like this could be a simple fix and I am just overlooking it. This is all written in c++ using the newest version of visual studios.

Upvotes: 0

Views: 925

Answers (2)

Jack Lilhammers
Jack Lilhammers

Reputation: 1247

There are 3 main bugs in your code, in order of appearance:

  • highest and otherHighest are not initialized
  • As pointed out by Kim Nyholm the salaries are used as indexes
  • Then highest and otherHighest are used as salaries instead of indexes
// 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";

Upvotes: 0

Kim Nyholm
Kim Nyholm

Reputation: 1115

You are wrongly using the salaries highest and otherHighest as indexes into the arrays baseballArray and basketballArray!

Upvotes: 2

Related Questions