Problem with functor in g++

I

IndyStef

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 said:
::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
 
J

John Carson

IndyStef said:
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);
 
I

IndyStef

Thank you John, that fixed it.

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++?

#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;

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top