473,503 Members | 1,804 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Problem with functor in g++

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<inta_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

Oct 23 '06 #1
2 1163
"IndyStef" <ok*******@hurco.comwrote in message
news:11**********************@b28g2000cwb.googlegr oups.com
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<inta_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
Oct 23 '06 #2
Thank you John, that fixed it.

On Oct 23, 8:17 am, "John Carson" <jcarson_n_o_sp_...@netspace.net.au>
wrote:
"IndyStef" <okrong...@hurco.comwrote in messagenews:11**********************@b28g2000cwb.g ooglegroups.com
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<inta_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
Oct 23 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
5915
by: Gert Van den Eynde | last post by:
Hi all, I'm struggling a bit with Functors generated for an ABC. This is the functor code: class Functor{ public: virtual double operator(double x)=0 }
8
3488
by: lok | last post by:
i have a class: template <class T1, class T2> class CPairMapping { public: typedef std::pair<T1, T2> ValuePair_t; typedef std::vector<ValuePair_t> ValueList_t; typedef std::binary_function<...
3
2146
by: CoolPint | last post by:
I have implemented a generic priority queue below and tested it works fine, but I have one small problem I cannot understand. I have type parameter F which determines the priority so that users can...
2
1986
by: Robbie Hatley | last post by:
I've got a function that I use a lot when making utility programs that need to do the same thing to every directory in a tree. Its prototype is: unsigned long int CursDirs (void Func(void)); ...
8
2680
by: Amit | last post by:
Hello all. If I want to use an object both as a Functor and also, if I pass a function pointer, how can it be done ? For instance, I have something like this template< typename Iter, typename...
8
1961
by: daniel.w.gelder | last post by:
Hello, I have been trying to write a functor template for a week now and I'm just having tons of trouble because I don't understand an issue that I guess is pretty basic to this task. ...
2
2501
by: Lionel B | last post by:
I have a function which takes a functor argument. I which to call it for a functor which is actually a class member; this works fine, using the mem_fun_ref and bind1st functions (see listing 1...
7
6246
by: DevNull | last post by:
Hi there everyone, I'm creating a very simple immediate mode command interpreter. The final purpose is to provide a pluggable control and command console for a MUD server I have written. The...
5
2514
by: Fei Liu | last post by:
Hello, I have a situation where I need to design a library for multi-thread application, each thread does some work in a client supplied std::ptr_fun(free_function) or a functor. Now since it's...
2
2269
by: aaragon | last post by:
Hi guys, Is there a way to return a functor from a recursive call that takes different paths? Let's say that I have a tree structure like: root | first child ---- nextSibling ----nextSibling...
0
7074
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7322
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6982
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7451
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
4667
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3150
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1501
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
731
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
374
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.