none
error C2064: term does not evaluate to a function taking 1 arguments

    Question

  • I am having this error msg
    error C2064: term does not evaluate to a function taking 1 arguments
    for lines 50 & 51.

    Those lines are:
        int maximum(const int* values, size_t numValues)
        {
            int tempMax;

            for (size_t i = 0; i < numValues; i++)
            {
                if(*values(i) > *values(i + 1))
                    tempMax = *values(i);
                else
                    tempMax = *(values + 1);        
            }

    Any help is greatly appreciated..

    Sunday, July 17, 2011 6:36 AM

Answers

  • Are you perhaps used to using VB?

    In VB arrays are done by using () but not in C/C++.

    In C/C++ [] is used for arrays, but the other thing to remember is that it also dereferences pointers.

    So if you use your function as something like

    int main()
    {
      int max;
      int arr[] = {0, 1, 2, 3, 4};
      max = maximum(arr, 5);
    
      return 0;
    }
    

    Then values[0] will be 0, values[1] will be 1 and so on. You don't need to add the extra * to get the value out.

    So your for loop would be more like

    for (size_t i = 0; i < numValues; i++)
    {
      if(values[i]) > values[i+1])
        tempMax = values[i];
      else
        tempMax = *(values + 1);     
    }
    

    But even then this still isn't right. There are two problems left, your array will go right out of bounds on the last loop, and the else part will actually set the tempMax to the value in values[1]. You also aren't taking into consideration the current value of tempMax, so if the array is 5, 4, 3, 2, 1, depending on how lucky you are the value in tempMax leaving the for loop will either be 1 or some weird number that you will have no idea how it got there. If I was to write the function it would be more like.

    int maximum(const int* values, size_t numValues)
    {
      int tempMax = 0x80000000; //initialise to a low value that you are going to get higher than
      //the value given here is the lowest negative number that int can store
    
      for (size_t i = 0; i < numValues; i++)
      {
        if(values[i] > tmpMax)
          tempMax = values[i]; //only update tempMax if the current value is larger than tempMax
      }
      return tempMax;
     }
    

    The for condition of numValues - 1 will make sure we never go past the end of the array. But this is assuming that your function is there to return the largest number in the array.

     


    This is a signature

    Any samples given are not meant to have error checking or show best practices. They are meant to just illustrate a point. I may also give inefficient code or introduce some problems to discourage copy/paste coding. This is because the major point of my posts is to aid in the learning process.
    Visit my (not very good) blog at
    http://ccprogramming.wordpress.com/
    • Marked as answer by Rob Pan Wednesday, July 27, 2011 8:53 AM
    Sunday, July 17, 2011 7:05 AM

All replies

  • "CObound850" <=?utf-8?B?Q09ib3VuZDg1MA==?=> wrote in message news:1d15601b-825c-4059-a137-e9ecd21452e0...

    I am having this error msg
    error C2064: term does not evaluate to a function taking 1 arguments
    for lines 50 & 51.

    Those lines are:
        int maximum(const int* values, size_t numValues)
        {
            int tempMax;

            for (size_t i = 0; i < numValues; i++)
            {
                if(*values(i) > *values(i + 1))
                    tempMax = *values(i);
                else
                    tempMax = *(values + 1);        
            }

    Any help is greatly appreciated..


    Indexing in C/C++ uses [index].  Function calls use (argument).
    Once a pointer is indexed, you do not need the indirection operator "*".
    And stylistically, I'd prefer not to mix *(pointer+index) and pointer[index].
    And ask yourself, if numValues is 5 (say) so the index goes from 0..4, what happens at the upper limit of your loop (when i is 4)?
    And how does the caller get the result of your function?
    How does this function handle the limiting case where numValues is zero?
     
    But apart from that, Mrs Lincoln, how did you find the play?
    Sunday, July 17, 2011 6:58 AM
  • Are you perhaps used to using VB?

    In VB arrays are done by using () but not in C/C++.

    In C/C++ [] is used for arrays, but the other thing to remember is that it also dereferences pointers.

    So if you use your function as something like

    int main()
    {
      int max;
      int arr[] = {0, 1, 2, 3, 4};
      max = maximum(arr, 5);
    
      return 0;
    }
    

    Then values[0] will be 0, values[1] will be 1 and so on. You don't need to add the extra * to get the value out.

    So your for loop would be more like

    for (size_t i = 0; i < numValues; i++)
    {
      if(values[i]) > values[i+1])
        tempMax = values[i];
      else
        tempMax = *(values + 1);     
    }
    

    But even then this still isn't right. There are two problems left, your array will go right out of bounds on the last loop, and the else part will actually set the tempMax to the value in values[1]. You also aren't taking into consideration the current value of tempMax, so if the array is 5, 4, 3, 2, 1, depending on how lucky you are the value in tempMax leaving the for loop will either be 1 or some weird number that you will have no idea how it got there. If I was to write the function it would be more like.

    int maximum(const int* values, size_t numValues)
    {
      int tempMax = 0x80000000; //initialise to a low value that you are going to get higher than
      //the value given here is the lowest negative number that int can store
    
      for (size_t i = 0; i < numValues; i++)
      {
        if(values[i] > tmpMax)
          tempMax = values[i]; //only update tempMax if the current value is larger than tempMax
      }
      return tempMax;
     }
    

    The for condition of numValues - 1 will make sure we never go past the end of the array. But this is assuming that your function is there to return the largest number in the array.

     


    This is a signature

    Any samples given are not meant to have error checking or show best practices. They are meant to just illustrate a point. I may also give inefficient code or introduce some problems to discourage copy/paste coding. This is because the major point of my posts is to aid in the learning process.
    Visit my (not very good) blog at
    http://ccprogramming.wordpress.com/
    • Marked as answer by Rob Pan Wednesday, July 27, 2011 8:53 AM
    Sunday, July 17, 2011 7:05 AM
  • I figured it out, before I checked here. It was to do with my pointer.
    It was offsetting the notation within my max func.

    But thanks for advice/help,
    -CObound.

    Sunday, July 17, 2011 7:21 AM