Home > Blockchain >  Why can't I pass operator to a function from an other in C ?
Why can't I pass operator to a function from an other in C ?

Time:05-23

In my C homework (where I have to short different arrays with different methods), I run into a problem. I can't pass comp() from one function to another.

Here is a simplified version of my code:

template <typename T, typename Compare = std::less<T>>
void fooFunction(T arr[], int arraySize, Compare comp = Compare{})
{
    int endElem=arraySize-1;
    int beginElem =0;
    fooFunction2(arr, beginElem , endElem, arraySize, comp()); //I am getting the errors here
}

template <typename T, typename Compare = std::less<T>>
void fooFunction2(T arr[], int beginElem, int endElem, int arraySize, Compare comp = Compare{})
{
    
}

struct string_size_less
{
    bool operator()(const std::string &a, const std::string &b) const
    {
        return a.size() < b.size();
    }
};

int main()
{
    int arrI[] = { 4, 5, 1, 4, 2};
    std::string arrS[] = {"Car", "Bicycle", "Metro", "Bike"};

    fooFunction(arrI, 5);
    fooFunction(arrS, 4, string_size_less());
    return 0;
}

At the moment I am getting:

error: no match for call to '(std::less<int>) ()'|
error: 'fooFunction2' was not declared in this scope, and no declarations were found by argument-dependent lookup at the point of instantiation [-fpermissive]|

What would be the correct way to solve this?

CodePudding user response:

I changed two things:

  1. in your call to fooFunction2(..), don't pass comp() but comp. If you add parantheses, you execute the comp-function, which fails because a comparing function needs arguments passed to it. Just passing comp instead will just tell it where to look for the function in question.

  2. place the definition of fooFunction2(..) above where you call it. If you define it lateron, the compiler won't find the definition at the moment it finds the first call to it. (There might be compiler options to fix this). Just swapping the defintions is enough to fix it, though declaring it in a header file (especially in larger projects) might be a smoother way. This code compiles just fine:

     template <typename T, typename Compare = std::less<T>>
     void fooFunction2(T arr[], int beginElem, int endElem, int arraySize, Compare comp = Compare{})
     {
    
     }
    
     template <typename T, typename Compare = std::less<T>>
     void fooFunction(T arr[], int arraySize, Compare comp = Compare{})
     {
         int endElem=arraySize-1;
         int beginElem =0;
         fooFunction2(arr, beginElem , endElem, arraySize, comp); //I am getting the errors here
     }
    
    
     struct string_size_less
     {
         bool operator()(const std::string &a, const std::string &b) const
         {
             return a.size() < b.size();
         }
     };
    
     int main()
     {
     int arrI[] = { 4, 5, 1, 4, 2};
     std::string arrS[] = {"Car", "Bicycle", "Metro", "Bike"};
    
     fooFunction(arrI, 5);
     fooFunction(arrS, 4, string_size_less());
     return 0;
     }
    

CodePudding user response:

The problem is that you're passing comp() as the last argument instead of passing comp.

To solve this pass comp instead of comp() as shown below:

struct string_size_less
{
    bool operator()(const std::string &a, const std::string &b) const
    {
        return a.size() < b.size();
    }
};

template <typename T, typename Compare = std::less<T>>
void fooFunction2(T arr[], int beginElem, int endElem, int arraySize, Compare comp = Compare{})
{
    
}
template <typename T, typename Compare = std::less<T>>
void fooFunction(T arr[], int arraySize, Compare comp = Compare{})
{
    int endElem=arraySize-1;
    int beginElem =0;
//----------------------------------------------------vvvv--->changed from comp() to comp
    fooFunction2(arr, beginElem , endElem, arraySize, comp);
}

Working demo

Moreover, before using a function you should have a declaration for that function. See in the working demo linked above.

  • Related