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.

 error msgs
--------------------------------------
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

LVL 1
shaolinfunkAsked:
Who is Participating?
 
sarabandeCommented:
The Functions::SetBoolToTrue(); is illegal cause you need an object of class Functions to call it.

   Functions func;
   func.SetBoolToTrue();  

this code compiles cause the non-static SetBoolToTrue now can set the non-static member bBool of the Functions object func.


statically declared functions can only set static member variables and arguments. so you can call static member function without object but only with class scope.


Sara
0
 
orcicCommented:
You should instantiate an object of your class and then call it's method, or else declare the method or your class as static.
In other words you could try
void CExampleDlg::OnBnClickedButton2()
{
      // TODO: Add your control notification handler code
     Functions func = new Functions();
     func.SetBoolToTrue();
}
0
 
shaolinfunkAuthor Commented:
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?
0
Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

 
phoffricCommented:
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();  
0
 
shaolinfunkAuthor Commented:
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?
0
 
sarabandeCommented:
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

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

Sara
0
 
sarabandeCommented:
           Functions ptrFunctionsObject = new Functions;

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

Sara
0
 
orcicCommented:
yep, it was c# sintax -> sorry for confusion
0
 
phoffricCommented:
oops, I left out a *:
Amended:
    Functions * ptrFunctionsObject = new Functions();

If making SetBoolToTrue() function static, then you could either have bBool defined as a global variable (i.e., outside of classes and all functions), or you could define bBool as a static member within the Functions class. However, if doing the latter, then all instantiated objects of the Functions class will share this single bBool value. If that is acceptable to you (or if you only plan on ever having one instantiated object of the Functions class, then you could make bBool static as well. (And if you do make bBool static, then you will have to define the memory for bBool, typically in a .cpp file.)
0
 
sarabandeCommented:
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
0
 
shaolinfunkAuthor Commented:
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?

0
 
shaolinfunkAuthor Commented:
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.
0
 
orcicCommented:
0
 
shaolinfunkAuthor Commented:
Thank you all.  I am going to ask another related question ina separate post...it's 2) in my post to Phoffric.
0
 
phoffricCommented:
>> 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;

0
 
shaolinfunkAuthor Commented:
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.