Hi,
I've been working on a message system using threading: -
class console {
-
private:
-
HANDLE OutThread;
-
char * out;
-
bool Loopflag;
-
void MessageLoop(void *){
-
for (;!Loopflag;){
-
cout << out;
-
out = "";
-
Sleep(1000);
-
}
-
}
-
public:
-
void write(char* message){ out=message;}
-
void start(){ OutThread = (HANDLE)_beginthread(MessageLoop,0,this);}
-
}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
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.
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 ===| 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?
I tried that, and I get the following error:
It's common techinique - declare MyClass* me variable and cast from void * to it, like -
static void Class::method(void*arg)
-
{
-
Class *me = reinterpret_cast<Class*>(arg);
-
me->privatemember = 0;
-
...
-
}
-
Sorry, what do you mean by singleton class?
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 )
Savage 1,764
Recognized Expert Top Contributor
It's common techinique - declare MyClass* me variable and cast from void * to it, like -
static void Class::method(void*arg)
-
{
-
Class *me = reinterpret_cast<Class*>(arg);
-
me->privatemember = 0;
-
...
-
}
-
Then he cannot use that pointer for any additional data he needs.
I couldnt get that working anyway.
I made all the functions and variables static: - class console {
-
private:
-
static HANDLE OutThread;
-
static char * out;
-
static bool Loopflag;
-
static void MessageLoop(void *){
-
while(!Loopflag){
-
cout << out;
-
out = "";
-
Sleep(1000);
-
}
-
}
-
public:
-
static void write(char* message){ out=message;}
-
static void start(){ OutThread = (HANDLE)_beginthread(MessageLoop,0,0);}
-
}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.
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
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?
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.
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: - class console
-
{
-
//...static members and other stuff
-
-
};
-
-
-
bool console::Loopflag=false;//or true if that's what you want.
-
char *console::out=0;
-
HANDLE console::OutThread=0;
Doing that, as it is after the class, the MessageLoop() function calls to undefined Loopflag and out.
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 )
I've never used step into before, and dont know how.
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?
I can set breakpoints, and I can see step into and step out under the debug menu.
I have not read the documentation.
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: - static void MessageLoop(void *)
-
{
-
-
while(!Loopflag)
-
{
-
cout << out;
-
out = "";
-
Sleep(1000);
-
}
-
}
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.
I tried that, but it still lists 'out' as an error.
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)?
I tried that, with static, and it still does not work.
Wasnt there a pointer method of this?
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: - #include <iostream>
-
#include <windows.h>
-
#include <process.h>
-
-
using namespace std;
-
-
-
class console {
-
private:
-
//..............(Members)
-
static void MessageLoop(void *){
-
-
while(!Loopflag){
-
-
cout << out;
-
Sleep(1000);
-
}
-
}
-
public:
-
//.........(Methods)
-
-
}console;
-
-
bool console::Loopflag=false;
-
char *console::out=0;
-
HANDLE console::OutThread=0;
-
-
-
int main()
-
{
-
console.start();
-
console.write("\nHello,World!!");
-
system("pause");
-
return 0;
-
}
Yay!!!, thanks a lot savage.
Savage 1,764
Recognized Expert Top Contributor
Yay!!!, thanks a lot savage.
Isn't it this the same code you have had?
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.
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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...
|
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
|
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...
|
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...
|
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...
|
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.
...
|
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,...
|
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,...
|
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...
|
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...
|
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,...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |