0

I'm new at C++ and I'm trying to use find_if with templates but it doesn't seem to work the way I want it to. Why is that? I tried to find the answer in previous asked questions about templates with iterators, but I guess I missed the right one or maybe just didn't understand the answers correctly. I tried to use typename before iterator, but that didn't change the error-message.

Is there a better way to do this and if so, can someone help me to learn how to do this?

(error message: error C3867: 'UserInterface::Number': function call missing argument list, use '&Userinterface::Number' to create a pointer to member) =

When that happens, I know that I have missed () after the function call, but thats not the case this time?!

#include <iostream>     // std::cout
#include <algorithm>    // std::find_if
#include <vector>       // std::vector

template<typename T>
class UserInterface
{
public:
bool Number(int i);
void function();
};

template<typename T>
bool UserInterface<T>::Number(int i) {
return (i >= 40);
}

template<typename T>
void UserInterface<T>::function()
{
std::vector<T> myvector;

myvector.push_back(10);
myvector.push_back(25);
myvector.push_back(15);
myvector.push_back(55);
myvector.push_back(1);
myvector.push_back(65);
myvector.push_back(40);
myvector.push_back(5);

std::vector<T>::iterator it = std::find_if(myvector.begin(), myvector.end(), Number);
std::cout << "The first value over 40 is " << *it << '\n';

std::cin.get();
}

int main() {
UserInterface<int> fu;
fu.function();

return 0;
}
Ðаn
  • 10,934
  • 11
  • 59
  • 95

1 Answers1

0

There are a few problems with your example. The first is that std::find_if is incompatible with non-static member method pointers. Those pointers would require a this to work. Since UserInterface::Number doesn't access any non-static members and doesn't call any non-static methods, you can just make it static.

The second issue is that you must use & to obtain a pointer to your function.

Finally, don't forget typename before std::vector<T>::iterator.

#include <iostream>     // std::cout
#include <algorithm>    // std::find_if
#include <vector>       // std::vector

template<typename T>
class UserInterface
{
public:
    static bool Number(int i);
//  ^^^^^^ Add static here
    void function();
};

template<typename T>
bool UserInterface<T>::Number(int i) {
    return (i >= 40);
}

template<typename T>
void UserInterface<T>::function()
{
    std::vector<T> myvector;

    myvector.push_back(10);
    myvector.push_back(25);
    myvector.push_back(15);
    myvector.push_back(55);
    myvector.push_back(1);
    myvector.push_back(65);
    myvector.push_back(40);
    myvector.push_back(5);

    typename std::vector<T>::iterator it = 
//  ^^^^^^^^ typename here
    std::find_if(myvector.begin(), myvector.end(), &Number);                                            
//                                                 ^
    std::cout << "The first value over 40 is " << *it << '\n';

    std::cin.get();
}

int main() {
    UserInterface<int> fu;
    fu.function();

    return 0;
}
Ðаn
  • 10,934
  • 11
  • 59
  • 95
François Andrieux
  • 28,148
  • 6
  • 56
  • 87