Error C2352 - illegal call of non-static member function 
Author Message
 Error C2352 - illegal call of non-static member function

Dear All,

I'm quite a newbie at C++ and MFC in particular so please bear with me.

I've generated an app framework with AppWizard for a dialog based app. I
then added a file to the project which contains some C functions that form
the basis of the app. When I tried to call a function that was defined in a
class of the generated app I got the error 'C2352 - illegal call of
non-static member function'. Can someone tell me what I'm doing wrong (or
what I haven't done?). From reading the explanation of the error I think
that the functions in my added file are being treated as 'static' (am I
right in this?)

Code snippets:

In file PmtDlg.h, the appwizard generated class:

class CPmtDlg : public CDialog
{
// Construction
public:
 void SetPrintAlign(CDC *, HDC);
 CPmtDlg(CWnd* pParent = NULL); // standard constructor

...  etc

In my added file PmtEngine.cpp

void PrintTotals(CDC *pDC, HDC hdcPrn)
{
    ..... init stuff

    CPmtDlg::SetPrintAlign(pDC, hdcPrn);            <- this line generates
the error

    ..... more stuff

Quote:
}

I can get round this by putting 'static' in fron of the declaration of
SetPrintAlign in the class but I have a feeling that this should not be
needed and I'm doing something wrong somewhere else.

Any help would be most appreciated.

John Spink



Wed, 07 Nov 2001 03:00:00 GMT  
 Error C2352 - illegal call of non-static member function

Hi,
From my understanding of this problem it is one of context.

Non static memebers and functions of a class are called for an object of a class.
They have a 'this' pointer which tells them which object they are working on.
Thus you cannot call the member function as you have done. To use the function you would have to declare an object of PMTDlg and then call the function using the dot or funtion pointer notation.
For Example:
PMTDlg adlg;
adlg.SetPrintAlign(pDC, hdcPrn);

Static functions of a class do not have a 'this' pointer and that is why when you made the function static it worked because static functions do not need to be told which object they are working on.
However, static functions of a class CANNOT access members of the class (remember - no 'this' pointer')and other restrictions.

I hope this is of use - but this subject usually takes up a few pages in any C++ book - so i suggest you lookup help on staic members in VC help.

Andy

Quote:

>Dear All,

>I'm quite a newbie at C++ and MFC in particular so please bear with me.

>I've generated an app framework with AppWizard for a dialog based app. I
>then added a file to the project which contains some C functions that form
>the basis of the app. When I tried to call a function that was defined in a
>class of the generated app I got the error 'C2352 - illegal call of
>non-static member function'. Can someone tell me what I'm doing wrong (or
>what I haven't done?). From reading the explanation of the error I think
>that the functions in my added file are being treated as 'static' (am I
>right in this?)

>Code snippets:

>In file PmtDlg.h, the appwizard generated class:

>class CPmtDlg : public CDialog
>{
>// Construction
>public:
> void SetPrintAlign(CDC *, HDC);
> CPmtDlg(CWnd* pParent = NULL); // standard constructor

>....  etc

>In my added file PmtEngine.cpp

>void PrintTotals(CDC *pDC, HDC hdcPrn)
>{
>    ..... init stuff

>    CPmtDlg::SetPrintAlign(pDC, hdcPrn);            <- this line generates
>the error

>    ..... more stuff
>}

>I can get round this by putting 'static' in fron of the declaration of
>SetPrintAlign in the class but I have a feeling that this should not be
>needed and I'm doing something wrong somewhere else.

>Any help would be most appreciated.

>John Spink


-----------------** -- Posted from CodeGuru -- **-----------------
http://www.codeguru.com/    The website for Visual C++ programmers.


Wed, 07 Nov 2001 03:00:00 GMT  
 Error C2352 - illegal call of non-static member function
Thanks for that Andy, it has cleared things up (a bit!) but I can now call
the function in the way you suggested.


Quote:

> Hi,
> From my understanding of this problem it is one of context.

> Non static memebers and functions of a class are called for an object of a
class.
> They have a 'this' pointer which tells them which object they are working
on.
> Thus you cannot call the member function as you have done. To use the

function you would have to declare an object of PMTDlg and then call the
function using the dot or funtion pointer notation.
Quote:
> For Example:
> PMTDlg adlg;
> adlg.SetPrintAlign(pDC, hdcPrn);

> Static functions of a class do not have a 'this' pointer and that is why

when you made the function static it worked because static functions do not
need to be told which object they are working on.
Quote:
> However, static functions of a class CANNOT access members of the class

(remember - no 'this' pointer')and other restrictions.
Quote:

> I hope this is of use - but this subject usually takes up a few pages in

any C++ book - so i suggest you lookup help on staic members in VC help.
Quote:

> Andy



Wed, 07 Nov 2001 03:00:00 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Compiler Error C2352: illegal call of non-static member function

2. Illegal call of non-static member function (error)?

3. illegal call of non-static member function

4. illegal call of non-static member function

5. illegal call of non-static member function *HELP*

6. illegal call of non-static member function

7. illegal call of non-static member function

8. illegal call of non-static member function

9. Compiler Error C2352:<Function_Name> illegal call of non-static member function

10. iilegal call of non-static member function

11. One Problem in Calling Static function from Non static function

12. PostMessage compile error: non-static member function

 

 
Powered by phpBB® Forum Software