finding median of an array


I'm having problems with an error saying "invalid operands" for the line "m= a[median]+a[median-1];"
1
2
3
4
5
6
7
8
9
10
11
12
13
14
 void median(DollarAmount a[], int size){ // calculates the median of the array
    int median;
    if(size%2==!0){
        median= size/2;
        cout << "The median is " << a[median] <<endl;
    }
    else{
        median= size/2;
        DollarAmount m;
        m= a[median]+a[median-1];
        m= m/2;
        cout << "The median is " << m <<endl;
    }
}

m = a[median] + a[median - 1];
invalid operands

"Operands" of an operator.

That line contains four operators:
* op- in median - 1. Integers, should not error.
* op[], array member indexing. Twice. Should not error.
* op= in m = result of expression. DollarAmount should have op= by default, unless you have prevented it.
* op+. How do you add DollarAmounts?

Overall, why can't you post the actual error message and save everybody's time?
Too little information to make an intelligent guess, so.......

Looks like DollarAmount might be a custom class. Do you have overload(s) for operator= and/or operator+?

If it is an alias for a builtin data type, like int or double, there shouldn't be an error.

Posting code that compiles as well as posting the entire error message you are getting will go a long way to help us be able to help you.
I used the overload operators and fixed the "invalid operands" error, but now I have a linking error that I have difficulty fixing. I'll post my program below
header DollarAmount.h


Last edited on
main.cpp

Last edited on
Last post is a duplicate of http://www.cplusplus.com/forum/beginner/262982/

Please don't use multiple threads to ask the same question.
Topic archived. No new replies allowed.