Solved

starting a function from a menu

Posted on 1998-09-28
7
309 Views
Last Modified: 2010-04-01
I want to activate a function from a menu.
The function is a member of a different class from the one that is invoked when going to the menu.
In the message pump I write:
BEGIN_MESSAGE_MAP(CMyNewApp, CWinApp)
      ON_COMMAND(ID_HELP, CWinApp::OnHelp)
      ON_COMMAND(ID_INIT, MyApp::Init())
END_MESSAGE_MAP()
As I wrote, MyApp is a different class and I get the compiling error:
'MyApp::Init' : illegal call of non-static member function.
I do not want to make Init a static function because this causes problems later on.
What solves this?
Thanks!!
0
Comment
Question by:gilbert_chang
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
7 Comments
 
LVL 8

Accepted Solution

by:
Answers2000 earned 50 total points
ID: 1173891
1. Illegal call to non-static because the way you wrote it doesn't say which CWinApp object (I know you know there's only one of these, but the compiler doesn't)

2. (CMyNewApp *)AfxGetApp())->Init() ;

and ((CMyNewApp *)AfxGetApp())->OnHelp() ;

3. It would be _best_ to put in the functions in the right place (e.g. in the doc or view or frame) and then call using tip #2

e.g.
void CMyView::OnHelp( ..etc .. )
{
((CMyNewApp *)AfxGetApp())->OnHelp() ;
}

0
 

Author Comment

by:gilbert_chang
ID: 1173892
Note that Init is not a member of CMyNewApp:
ON_COMMAND(ID_INIT, MyApp::Init())
it is a member of MyApp which is a totaly independent class.
Thanks.
0
 
LVL 8

Expert Comment

by:Answers2000
ID: 1173893
Sorry I missed understood

Ok then

MyApp::Init() still means a call to a static as it doesn't specify which object (it only specifies MyApp class, there could be more than one object of this class)

(A) EITHER

1. Put a pointer to a MyApp object in the CMyNewApp object

class
{
//etc
private:
  MyApp * m_pMyApp ;

 //etc
} ;

2. Initialize m_pMyApp with a pointer to the MyApp object - by
(a) If MyApp object exists, simply assign m_pMyApp to point to this object (e.g. in constructor)
or
(b) create MyApp object in your constructor, and delete it in your constructor
CMyNewApp::CMyNewApp( ... etc )
{
m_pMyApp = new MyApp ;
//etc
}

CMyNewApp::~CMyNewApp()
{
delete m_pMyApp ;
}

3. Add OnInit menu item to CMyNewApp class, in handler call :-

void CMyNewApp::OnInit()
{
  m_pMyApp->Init() ;
}


(B) OR

1. Modify MyApp to be a singleton class ( or derive class from it which is singleton if you don't/can't modify source for MyApp).  My favorite way to do this is :

in a .h

class MyAppObject : public MyApp
{
private:
  static MyAppObject m_This ;
 
  MyAppObject() { }

public:
  ~MyAppObject() { }
 
  MyAppObject& Get() { return *this ; }
}

In a .cpp

MyAppObject MyAppObject::m_This

2. Add OnInit handler to CMyAppObject

void CMyNewApp::OnInit()
{
  MyAppObject::Get().Init() ;
}

0
[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

 

Author Comment

by:gilbert_chang
ID: 1173894
Thanks a lot!!
Option (A)->2.(b) did it!
0
 
LVL 22

Expert Comment

by:nietod
ID: 1173895
Note answer2000's option (A) is a more detailed version of my suggestion.  I was assuming that the MyApp object was global, so you wouldn't need a pointer to it (In my example I declared a global object of type MyApp).  Other than that, it was the same idea.
0
 
LVL 8

Expert Comment

by:Answers2000
ID: 1173896
nietod - scrolling back I don't see any comments from you on this Q - except the one made on 28 Sept (beginning "Note...")

0
 
LVL 22

Expert Comment

by:nietod
ID: 1173897
This was the question that was asked twice.  My answer was on the other question.  Gilbert_chang didn't think my answer worked, but he must have made a mistake because it was fine and was the same as yours with the exception that I assumed the objects was a global variable.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

623 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question