views:

130

answers:

4

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.

A: 

Try this,

#include <iostream.h>
#include <algorithm.h>
anish
Please use the code formatting tag.
shuttle87
-1: 1. You spelled `algorithm` wrong. 2. `<iostream>` and `<algorithm>` are the correct names -- there are no `.h`s in the standard. 3. That doesn't even remotely answer the OP's question because (s)he already has those headers #included.
Billy ONeal
I lol at the `(s)he`. :D
nakiya
+1  A: 

You need to have:

#include <functional>

in your code.

EDIT: see Billy ONeil's answer for what to do after you fix this.

shuttle87
+1 -- this is an issue too, though the bigger deal is that `mem_fun_ref` returns an unary function rather than a binary function.
Billy ONeal
@Billy, yes this is true, but as it stands the code will not compile without this include. I upvoted your answer too.
shuttle87
@shuttle: As it stands the code will not compile at all, even with the include. :)
Billy ONeal
+1  A: 

I got it to work :D.

#include <iostream>
#include <algorithm>
#include <functional>

using namespace std;

class X
{
    public:
        void Print(int x)
        {
            cout << x << endl;
        }

        void Do()
        {
            int arr[] = {1, 2, 3, 4, 5};
            mem_fun1_t<void, X, int> m(&X::Print);
            binder1st<mem_fun1_t<void, X, int> > b(m, this);
            for_each(arr, arr+5, b);
        }
};

int main()
{
    X x;
    x.Do();
}
nakiya
The horrors of STL...
kotlinski
@kotlinski: no, the horrors of insisting on binding member functions
jalf
+4  A: 

Here's a simpler way to do it:

void Print(int x)
{
    cout << x << endl;
}

class X
{
    public:
        void Do()
        {
            int arr[] = {1, 2, 3, 4, 5};
            for_each(arr, arr+5, Print);
        }
};

Or perhaps, redefine Print as a functor, if you need it to store state:

struct Print {
    void operator()(int x)
    {
        cout << x << endl;
    }
};

class X
{
    public:
        void Do()
        {
            int arr[] = {1, 2, 3, 4, 5};
            for_each(arr, arr+5, Print());
        }
};

That saves you all the icky binding which is just painful to look at. (It also gives you a slimmer class, which is usually a good thing)

Although in this particular case, a more natural way to do it might actually be to ditch for_each entirely and just copy to the output stream:

int arr[] = {1, 2, 3, 4, 5};
std::copy(arr, arr+5, std::ostream_iterator<int>(std::cout));

The nice thing about the STL is that you have all sorts of algorithms, not just for_each. You can copy, transform (map), accumulate (fold/reduce) or a number of other algorithms depending on your needs. In this case, what you want to do is copy the contents of your array to a stream, and streams can be dressed up as iterators, allowing std::copy to work.

jalf
Good enough. Thanks.
nakiya
Added another possible solution using `std::copy`
jalf
Actually what I need is not to print out anything, I'll edit with the original code.
nakiya
Well, in general, if you want to copy one sequence to another (where a sequence might be a container, but also a stream of some kind, or anything else that can be represented by iterators), use `std::copy`. If you need to perform some kind of transformation on each element, and then storing the results in a sequence, use `std::transform`. Use whichever algorithm makes sense in your case, but use `for_each` as a last resort. If one of the other algorithms matches your needs, prefer those instead :)
jalf