473,513 Members | 3,621 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Herb Sutter's Example:CallBack,But there is a problem if i need a param

4 New Member
//the simple code
Expand|Select|Wrap|Line Numbers
  1. template<typename T>
  2. class CallBack
  3. {
  4. public:
  5. typedef void (T::*Func)(unsigned short);
  6.     CallBack(T *t, Func f):_t(t),_f(f)
  7.     {}
  8.     void operator()(unsigned short port)
  9.     {
  10.          _t->*_f(port);
  11.     }
  12. private:
  13.     T *_t;
  14.     Func _f;
  15. }
  16. //test base class
  17. class testBase
  18. {
  19. public:
  20. virtual ~testBase(){};
  21. virtual void do(unsigned short port) = 0;
  22. };
  23. //test class
  24. class Test: public testBase
  25. {
  26. public:
  27. void do(unsigned short port)
  28. {...}
  29. };
  30. //
  31. // std::list<unsigned short> someList;
  32. // std::list<Test*>::iterator it ...
  33. //
  34. for_each(someList.begin(),someList.end(),CallBack<testBase>(*it,&testBase::do));
  35. // has an error 
  36. // C2064: term does not evaluate to a function taking 1 arguments with VS2005
  37.  
does anyone know how to do this? thanks!
Apr 22 '07 #1
7 1507
JosAH
11,448 Recognized Expert MVP
You cannot name a function 'do', i.e. it's a reserved word.

kind regards,

Jos
Apr 22 '07 #2
Rester
4 New Member
You cannot name a function 'do', i.e. it's a reserved word.

kind regards,

Jos
sorry just a mistake, I mean just a Function Name here's code:
Expand|Select|Wrap|Line Numbers
  1. template<typename T>
  2. class CallBack
  3. {
  4. public:
  5. typedef void (T::*Func)(unsigned short);
  6.     CallBack(T *t, Func f):_t(t),_f(f)
  7.     {}
  8.     void operator()(unsigned short port)
  9.     {
  10.          _t->*_f(port);
  11.     }
  12. private:
  13.     T *_t;
  14.     Func _f;
  15. }
  16. //test base class
  17. class testBase
  18. {
  19. public:
  20. virtual ~testBase(){};
  21. virtual void doSomething(unsigned short port) = 0;
  22. };
  23. //test class
  24. class Test: public testBase
  25. {
  26. public:
  27. void doSomething(unsigned short port)
  28. {...}
  29. };
  30. //
  31. // std::list<unsigned short> someList;
  32. // std::list<Test*>::iterator it ...
  33. //
  34. for_each(someList.begin(),someList.end(),CallBack<testBase>(*it,&testBase::doSomething));
  35. // has an error 
  36. // C2064: term does not evaluate to a function taking 1 arguments with VS2005
  37.  
how to do it ? thanks
Apr 22 '07 #3
JosAH
11,448 Recognized Expert MVP
sorry just a mistake, I mean just a Function Name here's code:
Expand|Select|Wrap|Line Numbers
  1. template<typename T>
  2. class CallBack
  3. {
  4. public:
  5. typedef void (T::*Func)(unsigned short);
  6.     CallBack(T *t, Func f):_t(t),_f(f)
  7.     {}
  8.     void operator()(unsigned short port)
  9.     {
  10.          _t->*_f(port);
  11.     }
  12. private:
  13.     T *_t;
  14.     Func _f;
  15. }
  16. //test base class
  17. class testBase
  18. {
  19. public:
  20. virtual ~testBase(){};
  21. virtual void doSomething(unsigned short port) = 0;
  22. };
  23. //test class
  24. class Test: public testBase
  25. {
  26. public:
  27. void doSomething(unsigned short port)
  28. {...}
  29. };
  30. //
  31. // std::list<unsigned short> someList;
  32. // std::list<Test*>::iterator it ...
  33. //
  34. for_each(someList.begin(),someList.end(),CallBack<testBase>(*it,&testBase::doSomething));
  35. // has an error 
  36. // C2064: term does not evaluate to a function taking 1 arguments with VS2005
  37.  
how to do it ? thanks
The first parameter of your CallBack constructor call isn't correct; change the
for_each line to two lines:
Expand|Select|Wrap|Line Numbers
  1. TestBase tb;
  2. for_each(someList.begin(),someList.end(),CallBack<testBase>(&tb ,&testBase::doSomething));
kind regards,

Jos
Apr 22 '07 #4
Rester
4 New Member
The first parameter of your CallBack constructor call isn't correct; change the
for_each line to two lines:
Expand|Select|Wrap|Line Numbers
  1. TestBase tb;
  2. for_each(someList.begin(),someList.end(),CallBack<testBase>(&tb ,&testBase::doSomething));
kind regards,

Jos
(*it) means a pointer to a Test*

such as:
Expand|Select|Wrap|Line Numbers
  1. std::list<testBase*> testList;
  2. testList.push_back(new Test());
  3. std::list<unsigned short>::iterator it = testList.begin();
  4. //that should be correct,OK?
  5. for_each(someList.begin(),someList.end(),CallBack<testBase>((*it) ,&testBase::doSomething))
  6.  
Thanks
Apr 22 '07 #5
JosAH
11,448 Recognized Expert MVP
(*it) means a pointer to a Test*

such as:
Expand|Select|Wrap|Line Numbers
  1. std::list<testBase*> testList;
  2. testList.push_back(new Test());
  3. std::list<unsigned short>::iterator it = testList.begin();
  4. //that should be correct,OK?
  5. for_each(someList.begin(),someList.end(),CallBack<testBase>((*it) ,&testBase::doSomething))
  6.  
Thanks
No, now your iterator 'it' is an iterator over a list of unsigned shorts. I played a
bit with your code (and changed some names on the fly). This is what I got
(it works):
Expand|Select|Wrap|Line Numbers
  1. // in my .h file:
  2. template<class T>
  3. class CallBack
  4. {
  5. public:
  6.     typedef void (T::*func_t)(int);
  7.  
  8.     CallBack(T* t, func_t f):_t(t),_f(f) {}
  9.     void operator()(int p) {
  10.          (_t->*_f)(p);
  11.     }
  12.  
  13. private:
  14.     T*_t;
  15.     func_t _f;
  16. };
  17.  
  18. class DoFunc {
  19. public:
  20.        virtual void func(int i) { std::cout << i << std::endl; }
  21. };
I tested that bit with this:
Expand|Select|Wrap|Line Numbers
  1. // in my .cpp file:
  2.     vector<int> v;
  3.  
  4.     for (int j= 0; j < 10; j++)
  5.         v.push_back(j);
  6.  
  7.     vector<DoFunc*> df;
  8.  
  9.     df.push_back(new DoFunc());        
  10.     vector<DoFunc*>::iterator it= df.begin();
  11.  
  12.     for_each(v.begin(),v.end(),CallBack<DoFunc>(*it, &DoFunc::func));
kind regards,

Jos
Apr 22 '07 #6
Rester
4 New Member
god save me , how many mistake i have made;
in fact all i want to write is std::list<testBase*>::iterator it = testList.begin(),
but I …
sorry for my baby mistakes;
(_t->*_f)(p); //-----------------------I missed the brackets
Now it works fine , Thank you very much ,Jos.
Apr 22 '07 #7
JosAH
11,448 Recognized Expert MVP
(_t->*_f)(p); //-----------------------I missed the brackets
Now it works fine , Thank you very much ,Jos.
Oh dear, I missed it completely that your code didn't have the parentheses;
I just assumed you typed the same stuff as I did when I tested your idea ...
Well, we solved the problem by sheer serendipity then ;-)

kind regards,

Jos
Apr 22 '07 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

0
1378
by: Andrew | last post by:
I tried the following session example /**************The first page - page1.php *******************/ <?php // page1.php session_start(); echo 'Welcome to page #1';
5
4008
by: Rene Olsthoorn | last post by:
Dear readers, py2exe has a problem including libxml2. Not at building time, but at runtime. The libxml2.dll cannot be loaded... Is there anyone that NOT has the problem? (and can you drop me your setup.py scipt, please). Thanks in advance, Rene O.
1
1666
by: The Pistoleer | last post by:
I have an inconsistent problem with some Internet pages when using Netscape (7.1) that I cannot reproduce with IE nor locally. Below is an example link. There are several tables on the pages. The tables at the top containing small images are working correctly. The tables below that contain product descriptions and input to purchase an...
4
1346
by: Bob | last post by:
I cut and pasted the following from the HELP into the form load event of a new form and I get an error stating that MonthName is a namespace and can not be used as shown. What's wrong? Dim MyMonth As Integer Dim Name As String MyMonth = 4 Name = MonthName(MyMonth, True) ' "True" returns an abbreviated name. MsgBox(Name) ' Name...
2
1847
by: Alex | last post by:
Example uploaded to: http://www.clickatus.com/ajax/ BTW - This is for FIREFOX, won't work in IE. I don't know why but when it is executed the browser still in loading state... Even though XMLHttpRequest already got its string from the server... Any help?
1
1816
by: r2destini | last post by:
Hi Friends, I am new to .Net. So I don't know much. I am facing a problem in updating database through ADO.Net I am creating the dataset and there is no problem in the updation and deletion or insertion in the dataset but when I am updating the database through adaptor error occures (Coloured Red).
9
1718
by: optimistx | last post by:
Which url in your opinion would be a good or even the best example of javascript usage in a set of pages at least say 10 or more pages? How to use css, how to split js-code to files, how to code for good maintainability,objects, names, how to take care of older browsers, how to optimize the coding time, etc It might be easy to have strong...
46
2484
by: Bruce W. Darby | last post by:
This will be my very first VB.Net application and it's pretty simple. But I've got a snag in my syntax somewhere. Was hoping that someone could point me in the right direction. The history: My work involves creating custom packages of our software product for golf courses that purchase our software. The course data is kept as a back up in...
10
6927
by: SQACPP | last post by:
Hi, I try to figure out how to use Callback procedure in a C++ form project The following code *work* perfectly on a console project #include "Windows.h" BOOL CALLBACK MyEnumWindowsProc(HWND hwnd, LPARAM lparam) {
0
7269
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...
0
7177
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...
0
7559
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...
0
7542
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...
0
5701
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...
0
3248
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...
0
3237
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1611
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
1
811
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.