Link to home
Start Free TrialLog in
Avatar of shaolinfunk
shaolinfunkFlag for United States of America

asked on

Please help me create my first Class..?

Hi EE,

I created my first program in VS2008...a dialog-based MFC app called "Example".  By default, the app wizard create Example.cpp and ExampleDlg.cpp with their corresponding header files.

Then, I added a "Functions.cpp" and its header, as a place to put some future math functions.  But it's virtually empty right now.

Last, I added a simple button dialog that I can push to run a command.  My code is below...can someone help me understand the "illegal call of non-static member function" that I get when I compile?  Thank you.

 User generated image
--------------------------------------
ExampleDlg.cpp
--------------------------------------
#include "stdafx.h"
#include "Example.h"
#include "ExampleDlg.h"
#include "Functions.h"

void CExampleDlg::OnBnClickedButton2()
{
	// TODO: Add your control notification handler code
	Functions::SetBoolToTrue();
}
--------------------------------------
Functions.cpp
--------------------------------------
#include "Functions.h"
 
void Functions::SetBoolToTrue() 
{	 		
	 bBool = true;
}

--------------------------------------
Functions.h
--------------------------------------
class Functions
{
private:
	bool bBool;

public:  
	void SetBoolToTrue();
};

Open in new window

SOLUTION
Avatar of orcic
orcic

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Avatar of sarabande
sarabande
Flag of Luxembourg image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of shaolinfunk

ASKER

Ah ok.  I understand the concept of instantiate an object of my class and calling its method....Can you explain what your 2nd part is..declaring a method or class as static?
Avatar of phoffric
phoffric

If Functions is a class, and SetBoolToTrue() is a non-static member of the Functions class, then to invoke the SetBoolToTrue() function, you need either a pointer to the instantiated Functions object, or the Functions object itself.
For example (assuming a default constructor is available):

Functions myFunctionsObject;
...
myFunctionsObject.SetBoolToTrue();

or,

Functions ptrFunctionsObject = new Functions;
...
ptrFunctionsObject->SetBoolToTrue();

Now, if bBool is just a global variable, and not a member of the Functions class, then you could make SetBoolToTrue() a static function in the Functions class; and then you could invoke it using
     Functions::SetBoolToTrue();  
Thank you Orcic and Sara for your explanations.  I noticed a small variation in both of your answers.

Orcic: Functions func = new Functions();

vs.

Sara:   Functions func;

What does "= new Functions();" do?  Is it necessary?
orcic, in c++ your code does not compile cause the new operator would return a pointer of class Functions. also pointers need arrow operator.

       Functions *  func = new Functions();
       func->SetBoolToTrue();


Sara

the code orcic has posted was c# code (as far as i know).

Sara
           Functions ptrFunctionsObject = new Functions;

the code was wrong as well cause ptrFunctionsObject must be a pointer and not an object.

Sara
yep, it was c# sintax -> sorry for confusion
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
shaolinfunk, for the examples you had until now pointers are not necessary. pointers can be used for creating dynamic arrays or for pointing to baseclass objects for virtual use or for internal implementation of container classes.

Sara
Hi Phoffric!

1) So are you saying Orcic's code works the same as Sara's code because he created a pointer.... And that, according to you and Sara, the only error lay in the fact that he used  "func.SetBoolToTrue();" with a "." (dot) instead of "func->SetBoolToTrue();" with a "->" (arrow)?

2) I would like to try and make bBool into a global variable and understand that scenario as well.  How do I make bBool global?

Whoaa.. disregard my 1) please....6 replies have been posted in the time it took me to write the last post.  And so 1) is resolved.
Thank you all.  I am going to ask another related question ina separate post...it's 2) in my post to Phoffric.
>> disregard my 1)
Yes, so many replies..
Summary of . vs ->
If you create a pointer to a new Functions(), the use pointer-> to refer to the Functions members.
If you create an object (not using a pointer), for example,
   Functions myFunctionObject;
then myFunctionObject.SetBoolToTrue(); will work.


I looked at the link http://www.exforsys.com/tutorials/c-plus-plus/c-plus-plus-static-functions.html provided by orcic. I don't see how it could compile since the
    static int sum;
in the example class does not have memory for sum dedicated. (Maybe this once worked - it may be old code since they are using
#include <iostream.h>
instead of
#include <iostream>
but I don't know the history/evolution of C++ well enough to comment on that link.

In a .cpp file, you should dedicate some memory to sum as follows:
    int example::sum = 9;
or just
   int example::sum;