我是C++的新手,我试图在模板中使用find_if,但似乎没有按照我希望的方式工作。这是为什么?我试着在以前有关使用迭代器的模板问题中找到答案,但我想我错过了正确的答案,或者只是没有正确理解答案。我试图在迭代器之前使用typename,但这并没有改变错误信息。find_if()与模板?
有没有更好的方法来做到这一点,如果有的话,有人可以帮助我学习如何做到这一点?
(错误消息:错误C3867: '的UserInterface ::编号':函数调用缺少参数列表中,使用 '&用户接口::数' 来创建一个指针构件)=
当该发生了,我知道我已经错过了()函数调用后,但这不是这种情况?!
#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;
}
哪里和如何?我以为我尝试过,但我想我只是不明白如何。 :) – StudentLerning
您应该将Number函数声明为“static”。 – VTT
A)考虑使用lambda:'[](imt i){return i> = 40; }'b)'std :: vector myvector v = {10,25,...,5};' –
Borgleader