Hi, I am new to all the functional stuff in STL. I tried to do smething but it fails horribly no matter how I try it. Please comment:
#include <iostream>
#include <algorithm>
using namespace std;
class X
{
public:
void Print(int x)
{
cout << x << endl;
}
void Do()
{
int arr[] = {1, 2, 3, 4, 5};
mem_fun1_ref_t<void, X, int> oF = mem_fun_ref<void, X, int>(&X::Print);
binder1st<mem_fun1_ref_t<void, X, int> > oU = bind1st(oF, *this);
for_each(arr, arr+5, oU);
}
};
int main()
{
X x;
x.Do();
}
I get this error:
/usr/include/c++/4.3/backward/binders.h: In member function ‘typename _Operation::result_type std::binder1st<_Operation>::operator()(typename _Operation::second_argument_type&) const [with _Operation = std::mem_fun1_ref_t<void, X, int>]’:
/usr/include/c++/4.3/bits/stl_algo.h:3791: instantiated from ‘_Funct std::for_each(_IIter, _IIter, _Funct) [with _IIter = int*, _Funct = std::binder1st<std::mem_fun1_ref_t<void, X, int> >]’
main.cpp:19: instantiated from here
/usr/include/c++/4.3/backward/binders.h:121: error: no match for call to ‘(const std::mem_fun1_ref_t<void, X, int>) (const X&, int&)’
/usr/include/c++/4.3/bits/stl_function.h:632: note: candidates are: _Ret std::mem_fun1_ref_t<_Ret, _Tp, _Arg>::operator()(_Tp&, _Arg) const [with _Ret = void, _Tp = X, _Arg = int]
/usr/include/c++/4.3/backward/binders.h:121: error: return-statement with a value, in function returning 'void'
EDIT:
PluginLoader.h
#include <string>
#include <list>
#include <functional>
class Plugin;
//This class is an interface for loading the list of file names of shared objects.
//Could be by loading all file names in a dir, or by loading filenames specified in a file.
class FileNameLoader
{
public:
virtual std::list<std::string>& LoadFileNames() = 0;
};
class PluginLoader
{
public:
explicit PluginLoader();
virtual ~PluginLoader();
virtual bool Load();
virtual bool LoadPlugins(FileNameLoader&);
virtual bool LoadFunctions();
void SetLoadingPolicy(std::unary_function<std::string, void>*);
protected:
list<std::string> l_FileNames;
private:
explicit PluginLoader(const PluginLoader&);
PluginLoader& operator=(const PluginLoader&);
bool LoadSharedObjects();
void* LoadSharedObject(const std::string);
list<PluginFunction*> l_Functions;
list<Plugin*> l_Plugins;
std::unary_function<const std::string, void>*& p_LibLoader;
};
#endif // _PLUGIN_LOADER_HEADER_
PluginLoader.cpp
#include <PluginLoader.h>
#include <algorithm>
#include <dlfcn.h>
using namespace std;
//**************************************************************************************************
PluginLoader::PluginLoader():
{
mem_fun1_t<void, PluginFunction, int> oL(&PluginLoader::LoadSharedObject);
p_LibLoader = new binder1st<mem_fun1_t<void, PluginFunction, int> > oFunctor(oL, this);
}
//**************************************************************************************************
PluginLoader::~PluginLoader()
{
l_FileNames.clear();
l_Functions.clear();
l_Plugins.clear();
}
//**************************************************************************************************
bool PluginLoader::LoadSharedObjects()
{
for_each(l_FileNames.begin(), l_FileNames.end(), *p_LibLoader);
}
//**************************************************************************************************
void PluginLoader::LoadSharedObject(const std::string sFileName)
{
void* pHandle = dlopen(sFileName.c_str(), i_LibMode);
//if(pHandle == NULL)
//Check dlerror
}
//**************************************************************************************************
void PluginLoader::SetLoadingPolicy(unary_function<const string, void>*& pPolicy)
{
if(pPolicy != NULL)
{
delete p_LibLoader;
p_LibLoader = pPolicy;
}
}
I guess now it is ok.