Thread: invalid operands to binary?

  1. #1
    Registered User
    Join Date
    Dec 2012
    Posts
    1

    invalid operands to binary?

    The following lines of code from a function of mine is producing the output (this is just for a couple of the error lines but its all like this):

    hours_3b.c:151: error: invalid operands to binary + (have ‘float *’ and ‘float’)
    hours_3b.c:153: error: invalid operands to binary + (have ‘float *’ and ‘double’)

    Here is the code for the function:

    Code:
    if (employeeData[i].hours > STD_HOURS) {
      total_grossOT += employeeData[i].gross;
      /*Calculates sum total of gross pay */
      total_OT += (employeeData[i].hours - STD_HOURS);
      /*Calculates sum total of OT */
      total_hours += (employeeData[i].hours + employeeData[i].OT);
      /*Calculates sum total of hours */
    }
    
    else {
      employeeData[i].OT = 0;
    
      total_gross += (employeeData[i].wage_rate * employeeData[i].hours);
      /*Calculates the sum total of gross pay */
      total_hours += employeeData[i].hours;
      /*Calculates sum total of hours */
    }
    Im trying to figure out why this is happening. All of the variables being used are of type float.
    Last edited by Salem; 12-15-2012 at 10:39 PM. Reason: fixed tags

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Actually, let me put that a different way. Double check the type of thing that you are adding.

    c = a + b;
    For the best results, c, a and b should all be the same type.

    And that type needs to match exactly the type in the binary + operator declaration:
    Code:
    type& operator + (const type& a, const type& b);
    You're going to have confusing problems if type is different in places. (Like using pointers)
    Last edited by whiteflags; 12-15-2012 at 09:48 PM.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > All of the variables being used are of type float.
    Your error messages beg to differ.

    > hours_3b.c:151: error: invalid operands to binary + (have ‘float *’ and ‘float’)
    > hours_3b.c:153: error: invalid operands to binary + (have ‘float *’ and ‘double’)
    Something in each one of them is a pointer.

    Did you forget a subscript.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    What are the types of total_grossOT, total_OT, total_hours, etc.? As was mentioned by your compiler, the one on the left-hand side is a float *, not a float. Try changing the offending line to include a pointer dereference. For example, assuming line 151 has the total_grossOT line, then you can change it to

    *total_grossOT += some_float_arg;

    That kind of fix should at least remove the compiler error, but it may still have the wrong logic. Pointers must be pointing to some valid memory for that type before you dereference it. If you're just now discovering it's a pointer, you might have forgotten to do this.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting error: invalid operands to binary &
    By P6nn in forum C Programming
    Replies: 3
    Last Post: 05-06-2012, 04:27 AM
  2. error: invalid operands to binary %
    By OxKing033 in forum C Programming
    Replies: 12
    Last Post: 03-18-2008, 09:21 AM
  3. Invalid operands to binary *
    By jrfall349 in forum C Programming
    Replies: 8
    Last Post: 03-31-2007, 11:15 PM
  4. invalid operands to binary %
    By Mathsniper in forum C Programming
    Replies: 3
    Last Post: 12-03-2005, 11:21 PM
  5. invalid operands to binary ^ ?
    By seal in forum C Programming
    Replies: 14
    Last Post: 09-13-2005, 04:59 PM