Like Tree1Likes
  • 1 Post By iMalc

'error: invalid operands to binary *' problem

This is a discussion on 'error: invalid operands to binary *' problem within the C Programming forums, part of the General Programming Boards category; I've declared a matrix array but I've had some problems trying to solve this Code: float dot_product(double *, double *, ...

  1. #1
    Registered User
    Join Date
    Apr 2013
    Posts
    1

    'error: invalid operands to binary *' problem

    I've declared a matrix array but I've had some problems trying to solve this

    Code:
    float dot_product(double *, double *, size_t); 
    
    
    float 
    dot_product(double * weights[6][16], double * X[16], size_t n)
    {
            float Net[6]={0,0,0,0,0,0};
            size_t i;
     
            for (i = 1; i < 16; i++) {
                 Net[1] += weights[1][i] * X[i];
                 Net[2] += weights[2][i] * X[i];
                 Net[3] += weights[3][i] * X[i];
                 Net[4] += weights[4][i] * X[i];
                 Net[5] += weights[5][i] * X[i];
                 Net[6] += weights[6][i] * X[i];
            }
            return Net[6];
    
    
    }

    It produces the following errors

    Code:
    Line 42: error: conflicting types for 'dot_product'Line 38: error: previous declaration of 'dot_product' was hereIn function 'dot_product':Line 47: error: invalid operands to binary *Line 48: error: invalid operands to binary *Line 49: error: invalid operands to binary *Line 50: error: invalid operands to binary *Line 51: error: invalid operands to binary *Line 52: error: invalid operands to binary
    Any help?

  2. #2
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Code:
    float dot_product(double *, double *, size_t);
    is not a valid forward declaration for

    Code:
    float 
    dot_product(double * weights[6][16], double * X[16], size_t n)
    {
    Kurt

  3. #3
    Registered User
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    682
    Quote Originally Posted by Kevin Breslin View Post
    Code:
    float dot_product(double *, double *, size_t); 
    
    
    float 
    dot_product(double * weights[6][16], double * X[16], size_t n)
    {
            float Net[6]={0,0,0,0,0,0};
            size_t i;
     
            for (i = 1; i < 16; i++) {
                 Net[1] += weights[1][i] * X[i];
                 Net[2] += weights[2][i] * X[i];
                 Net[3] += weights[3][i] * X[i];
                 Net[4] += weights[4][i] * X[i];
                 Net[5] += weights[5][i] * X[i];
                 Net[6] += weights[6][i] * X[i];
            }
            return Net[6];
    }
    First, always keep declaration and definition same (copy-pasting is legitimate here):

    Code:
    float 
    dot_product(double * weights[6][16], double * X[16], size_t n);
    
    float 
    dot_product(double * weights[6][16], double * X[16], size_t n)
    {
        /* ... */
    }
    You should read more about array/pointer declarations. The first parameter "weights" is a two-dimensional array of "pointers to doubles" - not just "doubles". The second parameter isn't better.

    The first dimension of array parameter is meaningless, because user can always pass an array which is larger or smaller. However, you can keep it to make it a bit self-documenting.

    The thing necessary to do is to remove the asterix:

    Code:
    float
    dot_product(double weights[][16], double X[], std::size_t n);
    
    float
    dot_product(double weights[][16], double X[], std::size_t n)
    {
        /* ... */
    }
    Last edited by kmdv; 04-05-2013 at 03:24 PM.
    I never put signature, but I decided to make an exception.

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,308
    Arrays start at zero, not one. Valid indexes in this case are 0, 1, 2, 3, 4 and 5.

    You cannot return a local array, so I don't know what you plan on having this function do.

    I don't think you want any of those parameters to be pointers either.
    jwroblewski44 likes this.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

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, 05:27 AM
  2. Replies: 2
    Last Post: 12-26-2009, 10:07 AM
  3. Invalid Operands to Binary Error
    By MikeyVeeDubb in forum C Programming
    Replies: 17
    Last Post: 09-23-2009, 05:13 AM
  4. error: invalid operands to binary %
    By OxKing033 in forum C Programming
    Replies: 12
    Last Post: 03-18-2008, 10:21 AM
  5. error: invalid operands to binary *
    By cblix in forum C Programming
    Replies: 5
    Last Post: 01-29-2006, 08:45 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21