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
Solved

Please help me create my first Class..?

Posted on 2011-02-14
17
402 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 33

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
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
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 33

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 33

Expert Comment

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

Sara
0
 
LVL 33

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
 
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 33

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The viewer will learn how to use NetBeans IDE 8.0 for Windows to connect to a MySQL database. Open Services Panel: Create a new connection using New Connection Wizard: Create a test database called eetutorial: Create a new test tabel called ee…
The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.

829 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