i'm in this situation:
template<class T>
void testFuncMulti (set<T> setInput, bool (*f)() ) {
f();
}
bool func()
{
cout << "Hello!" << endl;
return true;
}
int main() {
set<string> testSet;
testSet.insert("XXX");
testFuncMulti(testSet, &func);
return 0;
}
What I would like to achieve is passing a parameter to the last function "func".
Tried in many many different ways but still nothing.
This is my attempt:
template<class T>
void testFuncMulti (set<T> setInput, bool (*f)(T) ) {
f(T);
}
template<class T>
bool func(T val)
{
cout << "Hello!" << endl;
return true;
}
int main() {
set<string> testSet;
testSet.insert("XXX");
testFuncMulti(testSet, &func(string("YYY")));
return 0;
}
Sorry i'm a c++ newbie...pls help!
UPDATE:
SOLVED!!
#include <iostream>
#include <functional>
#include <set>
using namespace std;
template<class T, class U>
void testFuncMulti (const set<T> setInput, U f) {
typename set<T>::iterator iter;
for(iter = setInput.begin();iter != setInput.end();++iter) {
f(*iter);
}
}
template<class T>
bool func(const T val)
{
cout <<val << "Hello!" << endl;
return true;
}
int main() {
set<string> testInput;
testInput.insert("XXX");
testInput.insert("XXX222");
testFuncMulti(testSet, func<string> );
return 0;
}