Solved

Please help me create my first Class..?

Posted on 2011-02-14
17
397 Views
Last Modified: 2013-12-14
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

0
Comment
Question by:shaolinfunk
  • 6
  • 5
  • 3
  • +1
17 Comments
 
LVL 7

Assisted Solution

by:orcic
orcic earned 150 total points
ID: 34887166
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
 
LVL 32

Accepted Solution

by:
sarabande earned 200 total points
ID: 34887172
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
 
LVL 1

Author Comment

by:shaolinfunk
ID: 34887173
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
 
LVL 32

Expert Comment

by:phoffric
ID: 34887177
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
 
LVL 1

Author Comment

by:shaolinfunk
ID: 34887184
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
 
LVL 32

Expert Comment

by:sarabande
ID: 34887188
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
 
LVL 32

Expert Comment

by:sarabande
ID: 34887195
the code orcic has posted was c# code (as far as i know).

Sara
0
 
LVL 32

Expert Comment

by:sarabande
ID: 34887204
           Functions ptrFunctionsObject = new Functions;

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

Sara
0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 7

Expert Comment

by:orcic
ID: 34887222
yep, it was c# sintax -> sorry for confusion
0
 
LVL 32

Assisted Solution

by:phoffric
phoffric earned 150 total points
ID: 34887225
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
 
LVL 32

Expert Comment

by:sarabande
ID: 34887228
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
 
LVL 1

Author Comment

by:shaolinfunk
ID: 34887229
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
 
LVL 1

Author Comment

by:shaolinfunk
ID: 34887235
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
 
LVL 7

Expert Comment

by:orcic
ID: 34887236
0
 
LVL 1

Author Closing Comment

by:shaolinfunk
ID: 34887306
Thank you all.  I am going to ask another related question ina separate post...it's 2) in my post to Phoffric.
0
 
LVL 32

Expert Comment

by:phoffric
ID: 34887315
>> 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
 
LVL 1

Author Comment

by:shaolinfunk
ID: 34887325
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.

707 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now