CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10
  1. #1
    Join Date
    May 1999
    Location
    SF Bay
    Posts
    51

    Don't understand why: "illegal call of non-static member function"

    I have a class that derives from CListCtrl called MyListCtrl, and another class that derives from CWnd called Utilities; they're in separate projects within the same workspace:


    // Utilities.h: interface for the Utilities class.
    //
    //////////////////////////////////////////////////////////////////////

    #if !defined(AFX_UTILITIES_H__D7DDA98F_2D94_11D4_B891_00508B35B83F__INCLUDED_)
    #define AFX_UTILITIES_H__D7DDA98F_2D94_11D4_B891_00508B35B83F__INCLUDED_

    #if _MSC_VER > 1000
    #pragma once
    #endif // _MSC_VER > 1000

    class Utilities : public CWnd
    {
    public:
    Utilities();
    virtual ~Utilities();
    int MessageBox(LPCTSTR pMessage, UINT pBoxStyle);
    };

    #endif // !defined(AFX_USERMSGBOX_H__D7DDA98F_2D94_11D4_B891_00508B35B83F__INCLUDED_)






    // Utilities.cpp: implementation of the Utilities class.
    //
    //////////////////////////////////////////////////////////////////////

    #include "Utilities.h"

    //////////////////////////////////////////////////////////////////////
    // Construction/Destruction
    //////////////////////////////////////////////////////////////////////

    Utilities::Utilities()
    {
    }

    Utilities::~Utilities()
    {
    }
    int Utilities::MessageBox(LPCTSTR pMessage, UINT pBoxStyle)
    {
    return ::MessageBox(NULL, pMessage, "Utilities", pBoxStyle);
    }




    In one of the functions of MyListCtrl I called Utilities::MessageBox:


    #include "..\Utilities\Utilities.h"
    .
    .
    void MyListCtrl::OnOpsAdd()
    {
    Utilities::MessageBox("OnOpsAdd", MB_OK);
    return;
    }




    I'm getting this compilation error that doesn't make alot of sense to me (but then I'm not that experienced of a VC++ programmer...)

    "error C2352: 'Utilities::MessageBoxA' : illegal call of non-static member function"

    Any idea?

    Thanks for any input.





  2. #2
    Join Date
    Jul 1999
    Location
    Pasadena, CA - USA
    Posts
    351

    Re: Don't understand why: "illegal call of non-static member function"

    Try


    void MyListCtrl::OnOpsAdd()
    {
    MessageBox("OnOpsAdd", MB_OK);
    return;
    }




    I don't think Messagebox(....) is a member function of your class.






    Regards,

    Paul Belikian

  3. #3
    Join Date
    Nov 1999
    Location
    Nova Scotia, Canada
    Posts
    59

    Re: Don't understand why: "illegal call of non-static member function"


    Utilities::MessageBox("OnOpsAdd", MB_OK);




    The problem with this line is that it is telling the compiler that it wants to call Utilities::MessageBox, but it doesn't provide an instance of Utilities with which to run it. A static function in Utilities would compile, because a static member (function or data) is only created once for each class, regardless of how many instances of the class are created.

    You need to have an instance of Utilities to call the function in your current implementation. So something like this will work:


    {
    ...
    // m_Utilities is defined as a private class member
    m_Utilities.MessageBox("OnOpsAdd", MB_OK);
    ...
    }




    Good luck,
    James


  4. #4
    Join Date
    Feb 2000
    Posts
    13

    Re: Don't understand why: "illegal call of non-static member function"

    If you want to make your function static change in utility.h:


    int MessageBox(LPCTSTR pMessage, UINT pBoxStyle);




    with:


    static int MessageBox(LPCTSTR pMessage, UINT pBoxStyle);




    and then you can use the code:


    Utilities::MessageBox("OnOpsAdd", MB_OK);




    Miguel.


  5. #5
    Join Date
    May 1999
    Location
    SF Bay
    Posts
    51

    Re: Don't understand why: "illegal call of non-static member function"

    I tried putting this in the MyListCtrl::OnOpsAdd function:


    Utilities m_Utilities;
    m_Utilities.MessageBox("OnOpsAdd", MB_OK);




    I got this compilation error:

    MyListCtrl.obj : error LNK2001: unresolved external symbol "public: virtual __thiscall Utilities::~Utilities(void)" (??1Utilities@@UAE@XZ)
    MyListCtrl.obj : error LNK2001: unresolved external symbol "public: int __thiscall Utilities::MessageBoxA(char const *,unsigned int)" (?MessageBoxA@Utilities@@QAEHPBDI@Z)
    MyListCtrl.obj : error LNK2001: unresolved external symbol "public: __thiscall Utilities::Utilities(void)" (??0Utilities@@QAE@XZ)

    Unfortunately that error doesn't speak alot to me what the problem is.

    Thanks.


  6. #6
    Join Date
    Nov 1999
    Location
    Nova Scotia, Canada
    Posts
    59

    Re: Don't understand why: "illegal call of non-static member function"

    You are probably not including Utilities.cpp in your project.

    It compiles fine, because the functions exist, it won't link beacuse there is no Utilities.obj for the linker to use. Basically the compiler says run this function, but the linker can't find the function to run it.

    Try including the .cpp, or try the other suggestion that was posted. I don't know if it will work either, unless you have at least one Utilities declared somewhere

    The reason you couldn't call it without an instance (unless it's static) is because there would be no this pointer...even though it isn't explicitly used by the function...


  7. #7
    Join Date
    May 1999
    Location
    ALABAMA, USA
    Posts
    9,917

    Re: Don't understand why: "illegal call of non-static member function"

    This as codegurulegs said or because you do not have implementation of this function, you have only a definition.
    I don’t know a reason you are trying to write your own MessageBox function while you have it already since your class is derived from CWnd. MessageBox is a non static member of CWnd!

    You can also use existing variety of flavors:
    · MessageBox API.
    · Global MFC AfxMessageBox.



    I don't do it for ratings. However, rating tells me how my solution worked and that is important.
    Good luck in your journey in a C++ land.

    There are only 10 types of people in the world:
    Those who understand binary and those who do not.

  8. #8
    Join Date
    May 1999
    Location
    SF Bay
    Posts
    51

    Re: Don't understand why: "illegal call of non-static member function"

    Utilities.h and Utilities.cpp are in a different project from MyListCtrl.cpp and MyListCtrl.h. I have "#include ..\Utilities\Utilities.h" on top of MyListCtrl.cpp.

    I did find something strange. When I open the Utilties project and try to compile Utilities.cpp, I get the error: "Cannot compile the file 'D:\Projs\Utilities\Utilities.cpp'; no compile tool is associated with the file extension". That is probably the reason why MyListCtrl is failing to call it. I don't understand why it wouldn't compile. The Utilities project contains exactly two files - Utilities.h and Utilities.cpp, and they are simple C++ source and header files as I posted.


  9. #9
    Join Date
    May 1999
    Location
    SF Bay
    Posts
    51

    Re: Don't understand why: "illegal call of non-static member function"

    I'm trying to write a common error dialog for our product. The reason I called MessageBox inside my Utilities::MessageBox was simply to make sure that I can call that function from any source file that has a pointer to the Utilities project files. The common error dialog will produce a MessageBox but it will be tailored to use our own icons, buttons and error log. That's why I'm not using simply CWnd::MessageBox or ::MessageBox.


  10. #10
    Join Date
    Feb 2000
    Location
    Phoenix, AZ
    Posts
    450

    Re: Don't understand why: "illegal call of non-static member function"

    This is not very object oriented. That is as bad as just making a global error log function. Instead of having a Utilities class with a MessageBox function, make a CErrorDialog class that is derived from CDialog. Utility class that do a whole bunch of miscellanous things is simply not good object oriented programming


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured