Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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?

>Solution :

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;
     }
    
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading