Easy c++ question, classes

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Post Reply
Hairan
Posts: 6
Joined: Tue Mar 24, 2009 12:42 pm

Easy c++ question, classes

Post by Hairan »

Hey!

So I have been trying to split my code into several files and using headers to declare the classes and then define them in a source file. The problem is in the Source file I can access all the public functions and define them nicely, but I am not able to use the private variables inside those functions. How can you solve this?

I get the following error:

Code: Select all

c:\users\johan\documents\visual studio 2008\projects\oop_test\oop_test\player.cpp(8) : error C2065: 'health' : undeclared identifier
1>c:\users\johan\documents\visual studio 2008\projects\oop_test\oop_test\player.cpp(14) : error C2065: 'health' : undeclared identifier
My code is:

Player.h

Code: Select all

#ifndef _PLAYER_H
#define _PLAYER_H

class Player
{
public:
	void set_health(int amount); // Public set function
	int get_health(); // Public get function

private:
	int health;
};

#endif
Player.cpp

Code: Select all

#include "Player.h"

// MY PLAYER CLASS

// define our set function
void set_health(int amount)
{
	health = amount;
}

// define our get function
int get_health()
{
	return health;
}
main.cpp

Code: Select all

#include "Player.h"
#include <iostream>


using namespace std;

/* Main function */
int main()
{
	Player player1;

	cout << "MAIN FUNCTION" << endl;

	cout << "Creating class: Player" << endl;
	player1.set_health(10);
	cout << "Player health: " << player1.get_health();

	int dummy;
	cin >> dummy;
}
I guess it is something really simple, tried googling and everything for hours without a good answer :P
Sylence
Posts: 725
Joined: Sat Mar 03, 2007 9:01 pm
Location: Germany
Contact:

Post by Sylence »

This is basic c++. You should learn this before using Irrlicht.

You can't access these variables because they belong to a class. And you create functions in your cpp file.

The syntax of an implementation of a method is:

Code: Select all

RETURN_TYPE CLASSNAME::METHOD( PARANETERS )
{
}
Software documentation is like sex. If it's good you want more. If it's bad it's better than nothing.
Hairan
Posts: 6
Joined: Tue Mar 24, 2009 12:42 pm

Post by Hairan »

Ye, I found that out, thougth I tried that, but I obviously forgot it somewhere along the road :P

Had it that way earlier, but had an other silly error and then forgot this when fixing the other error and then thought I tried it already.

Anyway thanks for quick reply :)
B@z
Posts: 876
Joined: Thu Jan 31, 2008 5:05 pm
Location: Hungary

Post by B@z »

Code: Select all

#include "Player.h"

// MY PLAYER CLASS

// define our set function
void Player::set_health(int amount)
{
   health = amount;
}

// define our get function
int Player::get_health()
{
   return health;
} 
coz these functions are members of the Player class, if you just write int get_health() that means you declare a new function, not just implementing the declared function
Image
Image
Systemerror
Posts: 98
Joined: Fri Oct 03, 2008 1:25 pm
Location: UK
Contact:

Post by Systemerror »

Also it's worth noting that because you're only using basic fuctions that returns a value, it might be easier for clarity and code consumption sake to use inline functions.


For instance:

Code: Select all

#ifndef _PLAYER_H 
 #define _PLAYER_H 

class Player 
{ 
public: 

       int get_health()                 { return health;}
       void set_health(int amount)     { health = amount;} 
       
private: 
   int* health; 

}Player1; //create instance 

#endif

As it seems pretty pointless giving it an implementation file for very simple code (unless you're going to change it of course).
-System error
Hairan
Posts: 6
Joined: Tue Mar 24, 2009 12:42 pm

Post by Hairan »

Ye that code was only a simple sample to get the grasp on classes. Easier to learn in small scale and then apply it to bigger projects once you get the concept :)
grumpymonkey
Posts: 222
Joined: Mon Jan 19, 2009 10:03 pm
Location: Miami, Florida
Contact:

Post by grumpymonkey »

you cant access private variables or functions directly, you must use a function to get them, and the function cant be initialized when an instance is initialized.

heres an example:

Code: Select all


class test{
private://this private line is unnecessary since classes have private initializations by default
	int health; //Private health variable

public:
	int get_health(void);
	void set_health(int newval);
};
int test::get_health(void){
	return test::health;
}
void test::set_health(int newval){
	test::health = newval;
}
then you can use it like this:

Code: Select all

#include<iostream>
using namespace std;
int main(){
test Player; //create instance of test called 'Player'
Player.set_health(500); //give him 500 health
cout << Player.get_health() << endl;
system("PAUSE");
return 0;
}
Image
Post Reply