I am trying to get the following to work( for practice ). Kinda psuedocode-ish
<template T>
class Foo{
//...
template<typename Func1>
void apply(const Func1&){
for each element in current Foo
Func1(*current)
}
void addFoo(const T& val){ ... } //add Node with value val.
Foo(const Foo<T>& f){
_fooSize = f._fooSize;
f.apply( std::mem_fun( &(Foo<T>::addObject)); //Problem arises or starts here I guess.
};
I wanted to use the apply algorithm in f.apply to call the current addObject with all values in f. This is the compile error:
error: no match for call to '(const std::mem_fun1_ref_t<void, List<int>, const int&>) (int&)'
note: candidates are: _Ret std::mem_fun1_ref_t<_Ret, _Tp, _Arg>::operator()(_Tp&, _Arg) const [with _Ret = void, _Tp = List<int>, _Arg = const int&]
I'm not quite sure how to solve this. Any help would be appreciated.
I'm not quite sure I understand what you're trying to do, but it looks like you might be forgetting to give the function a this pointer.
It would be helpful if I could compile your code myself, but without all the code that's not an option.
Here's some code which shows how you can use std::mem_fun, maybe it will help you solve your problem.
template <typename T>
class Foo
{
public:
void foo(T i)
{
//Make sure printf won't break in case T isn't an int.
static_assert(std::is_integral<T>::value, "T must be an integer");
printf("foo(%u)\n", i);
}
template <typename Func>
void runMemberFunction(Func func, T i)
{
//Run function with the this pointer as the first argument.
func(this, i);
}
};
int main(int, const char**)
{
Foo<int> myFoo;
auto fooFunc = std::mem_fun(&Foo<int>::foo);
myFoo.runMemberFunction(fooFunc, 42);
//Run the function with myFoo as the first argument (simulating the this pointer).
fooFunc(&myFoo, 42);
You need to bind the member function to an object instance. One method:
Foo(const Foo<T>& obj){
obj.apply(std::bind(std::mem_fn(&Foo<T>::pushBack), this, std::placeholders::_1));
}
You need to bind the member function to an object instance. One method:
Foo(const Foo<T>& obj){
obj.apply(std::bind(std::mem_fn(&Foo<T>::pushBack), this, std::placeholders::_1));
}
is there anyway I can do this without using the new features?
You could use boost or create your own function object that acts as a closure for the bound member function. Unfortunately, the C++03 standard library feature that would theoretically help with this, std::bind1st, doesn't play nicely with member functions that have reference parameters like your pushBack() member function. If you really do want to use that then you can alter your pushBack() member function to accept its argument by value and use:
obj.apply(bind1st(mem_fun(&Foo<T>::pushBack), this));
Also, you might want to initialize the pointers to null before you call the apply() member function.