Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Compiler Error C2352 (VC++ 6.0)

Options
  • 12-07-2004 1:17pm
    #1
    Registered Users Posts: 1,253 ✭✭✭


    Hey everyone,

    Right... More Visual C++ questions.

    What I want to do is to override the CMainFrame::OnFileOpen() function.

    Now, I have come accros this problem before. Basically what I want to do is to be able to check the filename that the user has entered to determine if it is the correct type of file or not. So I have overridden the OnFileOpen() function with one of my own. This new function checks the filename and if it is correct then calls CDocument::OnOpenDocument().

    However, upon compiling I recieve an error saying that I cannot call a non-static member function from a static member function. I have had this problem before but I cant remember what it means or how I got around it.

    Can anybody shed some light on this for me? Its confusing for me! :o :dunno: :(


Comments

  • Registered Users Posts: 1,931 ✭✭✭Zab


    Static methods are not associated with an instance of a class, just the class itself. To call a non-static method, the call has to be associated with an instance. So, if you just try to call non_static_method() from within a static method, you'll get an error like this. Actually, MSDN should have a page explaining the error.


  • Registered Users Posts: 1,253 ✭✭✭gobby


    Thanks for the reply Zab. Still, any ideas on how to get around this??


  • Registered Users Posts: 1,481 ✭✭✭satchmo


    Make the function that's doing the calling static too? That's only a hack though...

    There's really no easy way to get around static/non-static errors like this, it's more a question of how you design things in the first place. A few rules of thumb (from CoderSource.net):
    • A static member function can access only static member data, static member functions and data and functions outside the class. A non-static member function can access all of the above including the static data member.
    • static member function can be called, even when a class is not instantiated, a non-static member function can be called only after instantiating the class as an object.
    • A static member function cannot be declared virtual, whereas a non-static member functions can be declared as virtual.
    • static member function cannot have access to the 'this' pointer of the class.


  • Registered Users Posts: 1,253 ✭✭✭gobby


    Cheers for the input lads.

    Had a look at the problem with a fella in my office. We kinda came up with a workaround. Its not exactly ideal but it (just about) has the desired effect.

    Well, I'm happy anyways! :D


Advertisement