473,471 Members | 1,874 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Classes/Threading: (classname::)(void*)' does not match `void (*)(void*)'

83 New Member
Hi,
I've been working on a message system using threading:

Expand|Select|Wrap|Line Numbers
  1. class console {
  2.     private:
  3.     HANDLE OutThread;
  4.     char * out;
  5.     bool Loopflag;
  6.     void MessageLoop(void *){
  7.         for (;!Loopflag;){
  8.         cout << out;
  9.         out = "";
  10.         Sleep(1000);
  11.         }
  12.     }
  13.     public:
  14.     void write(char* message){ out=message;}
  15.     void start(){ OutThread = (HANDLE)_beginthread(MessageLoop,0,this);}
  16. }console;
But I get this error:
error: argument of type `void (console::)(void*)' does not match `void (*)(void*)'

I tried different ways of getting around this problem, like using MessageLoop() as a static function, but it didnt like using the 'out' char.

Can someone give me help with this please.


-Adam
Aug 8 '08 #1
25 5815
Savage
1,764 Recognized Expert Top Contributor
You can't take address of member function without some type of not recommended fiddling,but you can make your MessageLoop function static.
Aug 8 '08 #2
Adam01
83 New Member
I tried that, and I get the following error:

18|error: invalid use of member `console::Loopflag' in static member function|
20|error: from this location|
17|error: invalid use of member `console::out' in static member function|
21|error: from this location|
17|error: invalid use of member `console::out' in static member function|
22|error: from this location|
||=== Build finished: 6 errors, 0 warnings ===|
Aug 8 '08 #3
Savage
1,764 Recognized Expert Top Contributor
I tried that, and I get the following error:

18|error: invalid use of member `console::Loopflag' in static member function|
20|error: from this location|
17|error: invalid use of member `console::out' in static member function|
21|error: from this location|
17|error: invalid use of member `console::out' in static member function|
22|error: from this location|
||=== Build finished: 6 errors, 0 warnings ===|
It does that because static functions do not have this pointer.
Can this class be a singleton class?
Aug 8 '08 #4
newb16
687 Contributor
I tried that, and I get the following error:
It's common techinique - declare MyClass* me variable and cast from void * to it, like
Expand|Select|Wrap|Line Numbers
  1. static void Class::method(void*arg)
  2. {
  3.   Class *me = reinterpret_cast<Class*>(arg);
  4.   me->privatemember = 0;
  5.   ...
  6. }
  7.  
Aug 8 '08 #5
Adam01
83 New Member
Sorry, what do you mean by singleton class?
Aug 8 '08 #6
Savage
1,764 Recognized Expert Top Contributor
Sorry, what do you mean by singleton class?
Singleton patern

,or you can only turn your class members to be static,thus making them accessibly without this pointer.(Consequences )
Aug 8 '08 #7
Savage
1,764 Recognized Expert Top Contributor
It's common techinique - declare MyClass* me variable and cast from void * to it, like
Expand|Select|Wrap|Line Numbers
  1. static void Class::method(void*arg)
  2. {
  3.   Class *me = reinterpret_cast<Class*>(arg);
  4.   me->privatemember = 0;
  5.   ...
  6. }
  7.  

Then he cannot use that pointer for any additional data he needs.
Aug 8 '08 #8
Adam01
83 New Member
I couldnt get that working anyway.

I made all the functions and variables static:

Expand|Select|Wrap|Line Numbers
  1. class console {
  2.     private:
  3.     static HANDLE OutThread;
  4.     static char * out;
  5.     static bool Loopflag;
  6.     static void MessageLoop(void *){
  7.         while(!Loopflag){
  8.         cout << out;
  9.         out = "";
  10.         Sleep(1000);
  11.         }
  12.     }
  13.     public:
  14.     static void write(char* message){ out=message;}
  15.     static void start(){ OutThread = (HANDLE)_beginthread(MessageLoop,0,0);}
  16. }console;
And then another error occurs:
obj\Debug\main.o||In function `_ZSt17__verify_groupingPKcjRKSs':|
C:\Program Files\CodeBlocks\MinGW\bin\..\lib\gcc\mingw32\3.4. 5\..\..\..\..\include\c++\3.4.5\bits\locale_facets .tcc|2498|undefined reference to `console::out'|
||=== Build finished: 1 errors, 0 warnings ===|



Savage, would it be ok for you to correct the problem code?
So I could study the method you used.
Aug 8 '08 #9
edwardrsmith
62 New Member
First off, I can't tell if you are trying to compile it as c or c++. If it is c then I believe cout will not work (I program under linux so it may be different under windows). The other problem could be that you forgot to include the apropriate header file.

Hope that helped,
Edward
Aug 8 '08 #10
Savage
1,764 Recognized Expert Top Contributor
I tried this on VC++ 9 Express and it works.
Can you give us at which line is compiler complaining?
Have you pre-initialized static variables?
Aug 8 '08 #11
Adam01
83 New Member
Im using code::blocks, GNU GCC compiler, and the error is in locale_facets.tcc on line 2498.

Would it be ok to correct the the code on my first post?

Pre-initalized, as in declared them before the class? no.
Aug 8 '08 #12
Savage
1,764 Recognized Expert Top Contributor
Im using code::blocks, GNU GCC compiler, and the error is in locale_facets.tcc on line 2498.

Would it be ok to correct the the code on my first post?

Pre-initalized, as in declared them before the class? no.
Because static members are not tied to any object of specific class,but rather are tied to the type of object(class it self),you need to initialize them before any type of use.

This would do the tick:

Expand|Select|Wrap|Line Numbers
  1. class console
  2. {
  3.   //...static members and other stuff
  4.  
  5. };
  6.  
  7.  
  8. bool console::Loopflag=false;//or true if that's what you want.
  9. char *console::out=0;
  10. HANDLE console::OutThread=0;
Aug 8 '08 #13
Adam01
83 New Member
Doing that, as it is after the class, the MessageLoop() function calls to undefined Loopflag and out.
Aug 8 '08 #14
Savage
1,764 Recognized Expert Top Contributor
Doing that, as it is after the class, the MessageLoop() function calls to undefined Loopflag and out.
That's how it looks,but if you try stepping into the MessageLoop() with debugger you would see that it runs,it runs simultaneously with other threads and that's why it exits.Try putting a pause at end of your main,and change out="" to something else and you'll see that it works as expected.(That is if you don't have a debugger )
Aug 8 '08 #15
Adam01
83 New Member
I've never used step into before, and dont know how.
Aug 8 '08 #16
Savage
1,764 Recognized Expert Top Contributor
I've never used step into before, and dont know how.
Do you know how to set a breakpoint?
What does your compiler documentation has to say about it?
Aug 8 '08 #17
Adam01
83 New Member
I can set breakpoints, and I can see step into and step out under the debug menu.
I have not read the documentation.
Aug 8 '08 #18
Savage
1,764 Recognized Expert Top Contributor
I can set breakpoints, and I can see step into and step out under the debug menu.
I have not read the documentation.
OK,set the breakpoint here:

Expand|Select|Wrap|Line Numbers
  1. static void MessageLoop(void *)
  2.     {
  3.  
  4.         while(!Loopflag)
  5.         {
  6.           cout << out;
  7.           out = "";
  8.           Sleep(1000);
  9.         }
  10.     }
And you will see that it runs forever(unless you set LoopFlag to true),and that's just what it is supposed to do.

You can verify this by changing out="" to something else and see that new data gets printed every second.
Aug 8 '08 #19
Adam01
83 New Member
I tried that, but it still lists 'out' as an error.
Aug 8 '08 #20
Savage
1,764 Recognized Expert Top Contributor
I tried that, but it still lists 'out' as an error.
Oh sorry my bad,I've thought it runs(as it should be).
Have you tried to specify the scope of those variables(e.g instead out,console::out)?
Aug 8 '08 #21
Adam01
83 New Member
I tried that, with static, and it still does not work.
Wasnt there a pointer method of this?
Aug 8 '08 #22
Savage
1,764 Recognized Expert Top Contributor
I tried that, with static, and it still does not work.
Wasnt there a pointer method of this?
This is the code I have,and in which I'm 100% sure it works.
Create a new project and lets see whether error lies somewhere else:

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <windows.h>
  3. #include <process.h>
  4.  
  5. using namespace std;
  6.  
  7.  
  8. class console {
  9.     private:
  10.     //..............(Members)
  11.     static void MessageLoop(void *){
  12.  
  13.         while(!Loopflag){
  14.  
  15.             cout << out;
  16.             Sleep(1000);
  17.         }
  18.     }
  19.     public:
  20.    //.........(Methods)
  21.  
  22. }console;
  23.  
  24. bool console::Loopflag=false;
  25. char *console::out=0;
  26. HANDLE console::OutThread=0;
  27.  
  28.  
  29. int main()
  30. {
  31.     console.start();
  32.     console.write("\nHello,World!!");
  33.     system("pause");
  34.     return 0;
  35. }
Aug 8 '08 #23
Adam01
83 New Member
Yay!!!, thanks a lot savage.
Aug 8 '08 #24
Savage
1,764 Recognized Expert Top Contributor
Yay!!!, thanks a lot savage.
Isn't it this the same code you have had?
Aug 8 '08 #25
Adam01
83 New Member
Ohh, I see now, when I added the declarations after the class, I removed the declarations from the class. Which triggered the error.
Thanks for your help savage.
Aug 8 '08 #26

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

Similar topics

65
by: Anthony_Barker | last post by:
I have been reading a book about the evolution of the Basic programming language. The author states that Basic - particularly Microsoft's version is full of compromises which crept in along the...
3
by: USCode | last post by:
I've been poking around the wxPython website and am unclear if wxPython has a wrapper for ALL wxWidget classes or just the GUI-related classes? Does wxPython also provide a python wrapper for the...
3
by: Bryan Parkoff | last post by:
I have C++ Primer Third Edition -- Author Stanley B. Lippman and Josee Lajoie. I have been studying it for couple months however it does not provide a valuable information which it is about...
11
by: mem | last post by:
Concrete classes are ones that can be instantiated directly, as long as it doesn't have pure virtual function. However, people suggest that "Don't derive from concrete classes." Does this mean...
1
by: MacKenzie Mickelsen | last post by:
Hey everyone I am new to threading. Does a thread stop/die once the method attached to it ends? Also does anyone know where I might find a tutorial on threading? Thanks Kenzie
2
by: joye | last post by:
Hello, My question is how to use C# to call the existing libraries containing unmanaged C++ classes directly, but not use C# or managed C++ wrappers unmanaged C++ classes? Does anyone know how...
4
by: Chris Johnson | last post by:
Ill be the first to admit this is way beyond my current scope as a VB programmer but Im learning as I go. I playing with the new Threading features exposed by the 2.0 framework and have writting...
7
by: bonk | last post by:
Hello I am acessing a Dictionary<TKey,TValuefrom multiple threads and often in a foreach loop. While I am within one of the foreach loops the other threads must not modify the collection itself...
19
by: frankiespark | last post by:
Hello all, I was perusing the internet for information on threading when I came across this group. Since there seems to be a lot of good ideas and useful info I thought I'd pose a question. ...
0
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,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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
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
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,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
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...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
muto222
php
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.