473,651 Members | 3,032 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(cons t 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()(cons t int n)
{
cout << n << ", ";
}
};

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

a_int.initializ e(1,10);
a_int.do_loop(o utput());

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(outp ut)'
tst.cpp:35: note: candidates are: void A<T,
addition>::do_l oop(output_func tor&) [with output_functor = output, T =
int, addition = add<int>]
Thanks,

Stefan

Oct 23 '06 #1
2 1173
"IndyStef" <ok*******@hurc o.comwrote in message
news:11******** **************@ b28g2000cwb.goo glegroups.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(cons t 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()(cons t int n)
{
cout << n << ", ";
}
};

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

a_int.initializ e(1,10);
a_int.do_loop(o utput());

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.n et.au>
wrote:
"IndyStef" <okrong...@hurc o.comwrote in messagenews:11* *************** ******@b28g2000 cwb.googlegroup s.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(cons t 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()(cons t int n)
{
cout << n << ", ";
}
};
int main(int argc, char* argv[])
{
A<inta_int;
a_int.initializ e(1,10);
a_int.do_loop(o utput());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
5923
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
3498
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< ValuePair_t, ValuePair_t, bool> ValuePair_IsLess; void SortAscend(const ValuePair_IsLess& isLess_) {
3
2160
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 instantiate in the following ways PQueue<int> pq1; PQueue<int, Functor> pq2; // where Functor is a name of user-defined class I also added another constructor to accept a function pointer so that
2
1988
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)); This just applies the fuction Func to every subdirectory of the current directory. It works fine when I pass it pointers to regular void-void functions.
8
2696
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 Predicate> class MyOperation: public std::binary_function<Iter, Iter,bool> { public: bool operator() (const Iter &val1, const Itr &val2) const {
8
1975
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. functor<bool (long, long)> myFunctor; myFunctor = AFunctionOfThatPrototype; To get that much is elementary because the template can just store a (bool)(long,long) as a member variable in functor<T>. Here is the problem:
2
2511
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 below). Or, rather, it works fine as long as my member functor is const. The problem comes when I wish to use it for a *non*-const functor (see listing 2 below): *** Start listing 1 *************************************************** // test1.cpp
7
6254
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 basic theory is we wrap the functions we want exposed to the console in a function with a prototype of int func(State*)
5
2524
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 a multi-threaded application, naturally I want the call back functor to be created on a per thread basis. Suppose my thread is so defined template <typename servlet_type>
2
2283
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 ----nextSibling ---->0 | |
0
8361
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8278
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8701
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8466
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8584
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7299
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4144
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4290
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2701
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 we have to send another system

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.