Problem with functor in g++

Discussion in 'C++' started by IndyStef, Oct 23, 2006.

  1. IndyStef

    IndyStef Guest

    Hi,

    I am having trouble compiling some templated code that uses functors in
    g++. It compiles fine in VC8. Any idea what I could do to make this
    work with g++?

    code:

    #include <iostream>

    using namespace std;

    template <class T>
    class add
    {
    public:
    T& operator()(T& t1, const T& t_inc)
    {
    t1 += t_inc;
    return t1;
    }
    };

    template <class T, class addition = add<T> >
    class A
    {
    private:
    T m_start;
    T m_end;

    public:
    A(){}
    ~A(){}

    void initialize(const T& start, const T& end)
    {
    m_start = start;
    m_end = end;
    }

    template <class output_functor>
    void do_loop(output_functor& o)
    {
    T var = m_start;
    for ( int n = 0; var <= m_end; ++n )
    {
    o(var);
    addition()(var,static_cast<T>(1));
    }
    }
    };

    struct output
    {
    void operator()(const int n)
    {
    cout << n << ", ";
    }
    };

    int main(int argc, char* argv[])
    {
    A<int> a_int;

    a_int.initialize(1,10);
    a_int.do_loop(output());

    getchar();
    return 0;
    }



    Output from g++:

    tst.cpp: In function 'int main(int, char**)':
    tst.cpp:58: error: no matching function for call to 'A(int,add<int>
    >::do_loop(output)'

    tst.cpp:35: note: candidates are: void A<T,
    addition>::do_loop(output_functor&) [with output_functor = output, T =
    int, addition = add<int>]


    Thanks,

    Stefan
     
    IndyStef, Oct 23, 2006
    #1
    1. Advertising

  2. IndyStef

    John Carson Guest

    "IndyStef" <> wrote in message
    news:
    > Hi,
    >
    > I am having trouble compiling some templated code that uses functors
    > in g++. It compiles fine in VC8. Any idea what I could do to make
    > this work with g++?
    >
    > code:
    >
    > #include <iostream>
    >
    > using namespace std;
    >
    > template <class T>
    > class add
    > {
    > public:
    > T& operator()(T& t1, const T& t_inc)
    > {
    > t1 += t_inc;
    > return t1;
    > }
    > };
    >
    > template <class T, class addition = add<T> >
    > class A
    > {
    > private:
    > T m_start;
    > T m_end;
    >
    > public:
    > A(){}
    > ~A(){}
    >
    > void initialize(const T& start, const T& end)
    > {
    > m_start = start;
    > m_end = end;
    > }
    >
    > template <class output_functor>
    > void do_loop(output_functor& o)
    > {
    > T var = m_start;
    > for ( int n = 0; var <= m_end; ++n )
    > {
    > o(var);
    > addition()(var,static_cast<T>(1));
    > }
    > }
    > };
    >
    > struct output
    > {
    > void operator()(const int n)
    > {
    > cout << n << ", ";
    > }
    > };
    >
    > int main(int argc, char* argv[])
    > {
    > A<int> a_int;
    >
    > a_int.initialize(1,10);
    > a_int.do_loop(output());



    Here you are passing a temporary to a function that takes a non-const
    reference argument. Change do_loop to take a const reference and change
    output so operator() is a const operator.

    Alternatively, declare:

    output o;

    and then call

    a_int.do_loop(o);


    > getchar();
    > return 0;
    > }



    --
    John Carson
     
    John Carson, Oct 23, 2006
    #2
    1. Advertising

  3. IndyStef

    IndyStef Guest

    Thank you John, that fixed it.

    On Oct 23, 8:17 am, "John Carson" <>
    wrote:
    > "IndyStef" <> wrote in messagenews:
    >
    >
    >
    > > Hi,

    >
    > > I am having trouble compiling some templated code that uses functors
    > > in g++. It compiles fine in VC8. Any idea what I could do to make
    > > this work with g++?

    >
    > > code:

    >
    > > #include <iostream>

    >
    > > using namespace std;

    >
    > > template <class T>
    > > class add
    > > {
    > > public:
    > > T& operator()(T& t1, const T& t_inc)
    > > {
    > > t1 += t_inc;
    > > return t1;
    > > }
    > > };

    >
    > > template <class T, class addition = add<T> >
    > > class A
    > > {
    > > private:
    > > T m_start;
    > > T m_end;

    >
    > > public:
    > > A(){}
    > > ~A(){}

    >
    > > void initialize(const T& start, const T& end)
    > > {
    > > m_start = start;
    > > m_end = end;
    > > }

    >
    > > template <class output_functor>
    > > void do_loop(output_functor& o)
    > > {
    > > T var = m_start;
    > > for ( int n = 0; var <= m_end; ++n )
    > > {
    > > o(var);
    > > addition()(var,static_cast<T>(1));
    > > }
    > > }
    > > };

    >
    > > struct output
    > > {
    > > void operator()(const int n)
    > > {
    > > cout << n << ", ";
    > > }
    > > };

    >
    > > int main(int argc, char* argv[])
    > > {
    > > A<int> a_int;

    >
    > > a_int.initialize(1,10);
    > > a_int.do_loop(output());Here you are passing a temporary to a function that takes a non-const

    > reference argument. Change do_loop to take a const reference and change
    > output so operator() is a const operator.
    >
    > Alternatively, declare:
    >
    > output o;
    >
    > and then call
    >
    > a_int.do_loop(o);
    >
    > > getchar();
    > > return 0;
    > > }--

    > John Carson
     
    IndyStef, Oct 23, 2006
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. lok
    Replies:
    8
    Views:
    4,034
    Rob Williscroft
    Dec 2, 2003
  2. Joel
    Replies:
    2
    Views:
    804
  3. Lionel B
    Replies:
    2
    Views:
    514
    Lionel B
    Dec 1, 2006
  4. Fei Liu
    Replies:
    5
    Views:
    389
    Fei Liu
    Aug 21, 2007
  5. Fencer
    Replies:
    4
    Views:
    638
    Saeed Amrollahi
    Jul 25, 2011
Loading...

Share This Page