why the error message error:illegal call of non-static member func

hello,
        I have written my own function to perform some operation.I call it 
from  thread i have  created in my program.But on compilation it gives the 
following error
error:illegal call of non-static member function.
why such an error comes?Does it have to do with calling the function from a 
thread?This is what i think?.
When i declared the function as static it worked fine....
Can i know what was exactly going wrong and if i don't want it to be static 
what is the other solution?

Please help
0
sayu (83)
10/17/2005 9:05:03 AM
vc.mfc 33608 articles. 0 followers. Follow

3 Replies
1056 Views

Similar Articles

[PageSpeed] 26

sayu wrote:

> hello,
>         I have written my own function to perform some operation.I call it 
> from  thread i have  created in my program.But on compilation it gives the 
> following error
> error:illegal call of non-static member function.
> why such an error comes?Does it have to do with calling the function from a 
> thread?This is what i think?.
> When i declared the function as static it worked fine....
> Can i know what was exactly going wrong and if i don't want it to be static 
> what is the other solution?
> 
> Please help

The thread function must be static because it is called by the operating 
system, which does not support nonstatic C++ member functions.  If you 
wish to use or call a nonstatic function you must pass the object 
pointer to the thread, and use that to access the nonstatic function.

-- 
Scott McPhillips [VC++ MVP]

0
Scott
10/17/2005 6:28:50 AM
Threads are a lot like global functions and it seems as though you're calling 
a function from some sort of class not declared in the thread. You're likely 
going to have to use windows messages to communicate with the thread (search 
for it on msdn) or declare that class globally.
ie.
MyClass myClass;
bool AppFunction(){
...
}
thread {
}

as opposed to "MyClass myClass;" being written within another funtion or 
class.

Hope that helps.. threads are kind of tricky to learn.

"sayu" wrote:

> hello,
>         I have written my own function to perform some operation.I call it 
> from  thread i have  created in my program.But on compilation it gives the 
> following error
> error:illegal call of non-static member function.
> why such an error comes?Does it have to do with calling the function from a 
> thread?This is what i think?.
> When i declared the function as static it worked fine....
> Can i know what was exactly going wrong and if i don't want it to be static 
> what is the other solution?
> 
> Please help
0
Rob3660 (422)
10/17/2005 9:54:01 AM
That's what the compiler should say.  A static member function has no access to any
instance of the class, and consequently a member function which requires a class instance
will not compile.

class Whatever : public Something {
      protected:
	void SomeMethod();
	static UINT MyThreadFunction(LPVOID p);
};

/* static */ UINT  Whatever::MyThreadFunction(LPVOID p)
    {
     SomeMethod();
     return 0;
    }

Note that to call SomeMethod, you have to have an instance of the class Whatever.  There
is no instance of the class Whatever in MyThreadFunction, hence the complaint.

There are a couple solutions, depending on what you are trying to accomplish.  If
SomeMethod does not require access to any class methods or variables, you can make
SomeMethod static as well.  However, if it requires access to member variables or methods,
then it requires access to a particular instance of Whatever to get the appropriate class
members or to call other methods, you will need to pass in a pointer to the class:

AfxBeginThread(MyThreadFunction, this);

is the most common paradigm.  In the thread, you will do

/* static */ UINT Whatever::MyThreadFunction(LPVOID p)
    {
     Whatever * w = (Whatever *) p;
     // then you can write
     w->SomeMethod();
    ...
   }

Note that the most common case is that Whatever is a derived class from some CWnd class.
In such a case, you must (a) not manipulate any aspect of the CWnd associated with 'this',
that is, no SetWindowText, etc.  Only PostMessage can be used (it doesn't manipulate the
window, only the message queue) and (b) if any variable can be accessed concurrently by
the worker thread and the GUI thread you must provide explicit synchronization to make
sure there are no problems (try to avoid doing this!).  See my essay on worker threads on
my MVP Tips site for more details.
						joe

	
On Mon, 17 Oct 2005 02:05:03 -0700, "sayu" <sayu@discussions.microsoft.com> wrote:

>hello,
>        I have written my own function to perform some operation.I call it 
>from  thread i have  created in my program.But on compilation it gives the 
>following error
>error:illegal call of non-static member function.
>why such an error comes?Does it have to do with calling the function from a 
>thread?This is what i think?.
>When i declared the function as static it worked fine....
>Can i know what was exactly going wrong and if i don't want it to be static 
>what is the other solution?
>
>Please help
Joseph M. Newcomer [MVP]
email: newcomer@flounder.com
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm
0
newcomer (15973)
10/18/2005 3:23:37 PM
Reply:

Similar Artilces:

Item Message #2
I'd like to add an item message to a group of items, does anyone know if it's possible to do this, or does the item message box have to be activated item by item ??? thanks. marwat. We have a Utility for RMS Store & HQ. Amongst these are 36 new wizards one which allows you to associate a message to a whole group of item. If you want to know more contact me. Afshin Alikhani - [afshin@retailrealm.co.uk] CEO - Retail Realm = = = = = = = = = = = -- URL http://www.retailrealm.co.uk "marwat" wrote: > I'd like to add an item message to a group of items, does...

Persistent 'pop up' message problem
When Outlook Inbox is opened, the last message is highlighted and a pop up message appears asking where to save the (highlighted )message. No matter what reply is made - Cancel or <enter> - another box pops up asking the same thing. The only way to get out of Outlook is to open task manager and end the Outlook process. Needless to say, Outlook is not usable. I am using Vista. There was a file problem with a recent Vista installation on this computer: Upon reboot, Chkdsk began renaming the security id to a "default id" for all files. Many Thanks. Version of Outlook...

Windows 7 non-Beta joining to SBS2008 domain
I finally joined the computers I needed the old way. The http://connect wizard just doesn't work for me. It tells me that the computer I'm trying to join doesn't meet the requirements. Both the server and the workstations are up to date with all the MS patches. Any idea how to fix it? yaro This is a multi-part message in MIME format. ------=_NextPart_000_0455_01CA820D.E4FCD100 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable You need to install SBS Update Rollup (UR) 3 on the server. You should = then be able to...

Windows mail error message 12-18-09
What that means?: The message could not be sent. The authentication setting might not be correct for your outgoing e-mail [SMTP] server. For help solving this problem, go to Help, search for "Troubleshoot Windows Mail", and read the "I'm having problems sending e-mail" section. If you need help determining the proper server settings, please contact your e-mail service provider. The rejected e-mail address was 'phoenixgym@aol.com'. Subject 'World Champion Kayla Williams is competing in the Excalibur Cup 2010 (Virginia Beach)', Account: ...

Windows Media Player cannot play file because a network error occu
I am using Media Player 11 in Windows Vista and I cannot seem to play videos imbedded on websites. If someone sends me a WMV file in email, I can play it just fine. Just not ones on websites. I do not get an error code. Only a pop-up box that says what I have in the subject line - plus "The server might not be available. Verify that you are connected to the network and that your proxy settings are correct." A friend of mine also has MP 11 and Vista and it works just fine. On Fri, 13 Nov 2009 09:29:02 -0800, his1987 <his1987@discussions.microsoft.com&g...

Entourage Error 107
Recently, I have not been able to sent or receive corporate email from Entourage. Error: Am unknown error has occurred in Entourage Explanation: Mail could not be sent Account name: Sprint Exchange Account Error 107 Mac's are not supported by IT here. I have verified my settings in Entourage with other unsupported Corp Mac users and everything seems to be set correctly. I am running Mac Office/Entourage 2008 and have applied all available updates. Any help would be greatly appreciated. Thanks, -Greg Greg wrote: > Recently, I have not been able to sent or rec...

outlook message help
I recently set up microsoft outlook, I already had outlook express --same email same server some messages go to express and some go to ms outlook--why and how can I change this Thanks Use only one or other Apps. NOT both. If neccessary delete mail accounts in OE or ensure it is set not to retreive mail Do not try to delete OE as certain files are used elsewhere, not least the OE newsreader, which may be accessed directly from IE or OL. David "Integra" <integracareers@adelphia.net> wrote in message news:3735087C-71E5-4622-A70D-896AC44285FA@microsoft.com... > I recently set ...

Yahoo mail messages cause Outlook to crash
Some recent messages from Yahoo have caused my Outlook (Office 2003) to crash. I try to open the message, nothing happens for a while and then I get the window that says sorry, Outlook has to close, would I like to send an error message to Microsoft. Has anyone else had this problem? A friend noted that the Yahoo messages now include Yahoo! Messenger with Voice which seemed to be an embedded attachment and contains and HTML divider bar. No hidden graphic elements, however. Can somebody suggest a change of Outlook parameters that will prevent a crash? Thanks, Bill Edelstein Schenectady, NY ...

Re: 'Uknown Error 0x800CCC97'
I just heard back from the folks with whom I filed this bug. They say the bug is fixed in cppop 5.4 - request that your ISP upgrade to that. -- Jeff Stephenson Outlook Development This posting is provided "AS IS" with no warranties, and confers no rights "Jeff Stephenson [MSFT]" <stephenson@online.microsoft.com> wrote in message news:... > See the attached reply to another similar question. Your ISP's POP3 server > has a bug, and they should get a fixed version of the server. > > -- > Jeff Stephenson > Outlook Development > This posting...

Econnect error when same item on PO more than once when receiving.
DOC 30 ERROR: Microsoft.Dynamics.GP.eConnect: Number = 9344 Stored Procedure taPopRcptLineInsert : Description = PO Line number is required - item exists on more than one line. Does Econnect not support this scenario while the GP screens do? Thanks. Rich: I had the same type of error but awhile back but I was passing my document wrong when I was trying to update it. I had the same item number on seperate lines and of course I never passed the parameter @I_vPOLNENUM. Well of course eConnect didn't know which line to update thus I kept getting that 9344 error. After some t...

Error Message When Opening Excel #3
Everytime i open Excel or an Excel document, i get the following error message: "SUMIF.XLA is already open. Reopening will cause any change you made to be discarded. Do you want to reopen SUMIF.XLA?" Whether i answer YES or NO, the application will open normally. It is just very annouying.. HELP -- W Walid You could try looking under Tools>Add ins... and then uncheck anything, then check back those that were checked one at a time, close and re-open excel, when the error comes back, that's the faulty add-in, either go to the vendor or unload the add-in if you don&#...

Backing up messages in smtp queue
We're looking for different alternatives for disaster recovery and one option is to backup the smtp queue directory. What we'd like to do is to backup all smtp messages that come through before it hits the mailbox. This way we can redeliver messages in case of a failure. Just wondering if anyone knows of a third-party tool that can do this? So instead of backing it up, I imagine you really want to intercept the messages and write them to a seperate queue that never gets delivered until a disaster? I imagine that queue can get pretty large. There are plenty of third party option...

Access to Microsoft forums and error 13
I wanted to get a wider audience for the error 13 problem that may have been fixed with the latest download. I signed up for the Microsoft's green button, received a confirmation but can't log on. I probably have to wait for someone to come in on Monday. Meanwhile, could someone use the green button media center forum to confirm that the problem was introduced with the previous to last fix and the latest fix solved the problem. I can't verify the fix because I switched to cable and the problem is unique to dialup (switched lines). There's no information with ...

Is there any funtion which gets called when I click on the title bar of a dialog??
Hi all, After my MFC dialog gets displayed...........if I click on the desktop my dialog loses the focus.Once if I click on the title bar it regains the focus.The problem here is ,if I click on the dialog the tab gets set on the dialog.But as I am clicking on the title bar,the tab is not set & shortcut keys are not working. Note: The dialog has tabs. Can anybody suggest a funtion which gets called when I click on the Title Bar,so that I can set the tab & make short cut keys work. I am already using ONACTIVATE() to solve this tab problem in other scenarios,but this is not wor...

How to copy "Ignore Error" all through?
Hello I have a formula wherein the result is ok, but gets flagged "missing adjacent cells". I select the "Ignore this error" but how to copy this across all cells? Or do I have to do it on every cell? Thanks Select that whole range--make the active cell one of the cells with that error. Then click on the flag and choose "Ignore Error". dindigul wrote: > > Hello > I have a formula wherein the result is ok, but gets flagged "missing > adjacent cells". I select the "Ignore this error" but how to copy this > across all cells?...

Error in database....
A user posted a batch in payables management. After posting, there was an error encountered. It displays that the table updating was interrupted, use batch recovery to continue the posting. But when I used the batch recovery, it was not successful to continue the update process. When I click the "More Details" button it displays, A save operation on table 'PM_Transaction_WORK' caused a sharing error. How can I resolve this issue? Thanks, John John, it is a db sharing violation. Have all users logout DELETE tempdb..DEX_LOCK DELETE tempd..DEX_SESSION DELETE dynami...

Error Connecting
Using Outlook 2003 w/ Windows XP Pro, error occurs when attempting to download an email (spam) that has no subject line or sender address. I can download Inbox headers and delete the offending email, then continue email download. Have seen other threads w/ similar problem, but no solution found yet. Obviously I can work around it, but I get this type of spam 2 to 3 times a week (sometimes 2 or 3 a day). If I download 20 emails before it hits this snag, I either get to download them again, or go through the headers and delete the ones I've already received. Incapacitating? - No. Anno...

Event Error Question
Can anyone tell me what the foioloing event eree is and if there is a way to prevent it? Event filter with query "SELECT * FROM __InstanceModificationEvent WITHIN 60 WHERE TargetInstance ISA "Win32_Processor" AND TargetInstance.LoadPercentage > 99" could not be reactivated in namespace "//./root/CIMV2" because of error 0x80041003. Events cannot be delivered through this filter until the problem is corrected. I google the event adn got several different opinions, one being caused by root worms?? James ...

Setting up Message rules
I have read a few other post about setting up message rules, but I still can't get my messages to go into the folders I have set up. Here is what I have done. In message rules I have selected the Where the from line Contains -email-. Move it to -set- folder. I then go to apply now. I change "Storage Folders" to "Inbox" and then select Apply Now. Am I doing something wrong? This is for my work emails. I receive lots of emails from a couple different email address, and they are all POP3. Any help to get my folders set up would be great!! THANKS!! ...

ntuser.dat error message
When I bring up Outlook Express, I'm receiving the following error message: Cannot delete ntuser.dat: It is being used by another person or program. Close any programs that might be using the file and try again. This just started happening in the past few days. Outlook is the 1st thing (and only program that I know of) that is running. How can I make this stop. I'm lost. Regards Gerry Hi Gerry, I understand you are receiving the following error message when you open Outlook Express. "Cannot delete ntuser.dat: It is being used by another person or program, close any programs...

Font size when composing new message.....
Hi there, I changed my font options in the Tools Menu and it does not seem to help. When I am composing a new message, my font and signature , which includes a business card, is huge!!!! And also when I pick a theme for stationary that i had put on there, it is huge too! You can't see it all! What is going on, anybody? Thanks for any help!! I'm betting that it is your zoom settings in the email. For Outlook 2007, with the email open, click on the Format Text tab - Zoom and change the setting. For Outlook 2003 and older, with the email open, click on View - Zoo...

Sorting messages by Subject
Somthing interesting in Outlook. When you select a message and then try to sort messages by subject by clicking on the subject column, the message you originally chose is no longer selected. It defaults to the first "sorted-by-subject" message in your inbox. THis is not he case when you sort by the From field or the Received field. The message you originally chose before the sort by these fields still remains selected. Is there any workaround to this? We have installed Office SP1. Thanks, Marita ...

Outlook errors #2
When I start Outlook it immediately comes up with an error: msouplug.dll Does anyone have any advice on how to repair this fault or is there a way to get access to the contacts within Outlook if I cannot get it to open? Thank you Do you have any Symantec programs installed for Outlook? I believe that's where this file comes from. If you start Outlook in safe mode, you should be able to get in and remove that add-in. Close Outlook Select Start | Run Enter Outlook.exe /Safe Select OK Select Tools | Options | Other | Advanced Options | Add-In Manager Uncheck any Symantec add-in...

XML error "Some Data Was Imported As Text"
Daily, I recieve a 3rd party XML file that I parse in Excel (using an excel macro) and then import into an Access database. Infrequently, the data will import into Excel, then parse and then the routine stops with the error message above. I look through the raw XML file and everything appears to be within the brackets, etc. Can someone give me some clarification to the meaning of the error message? Thanks in advance and Mery Christmas and Happy Holidays! The error message might be generated within your macro - as it is parsing the data it might detect that some field which should be ...

iis smtp service messages stuck in queue
When I try to send email from the smtp service in iis on my workstation the messges get stuck in the queue. The even log shows ID 4000 Message delivery to the remote domain 'hotmail.com' failed for the following reason: The connection was dropped by the remote host. This occurs whether I'm trying to send to an outside address as above or to an account on my exchange server. This behaviour began after I migrated from Exchange 2000 to Exchange 2003. We also upgraded AD to 2003 at this time. Thanks for your help, Hugh Did you test name resolution? can you find hotmail.com using ns...