Jump to content

  • Log In with Google      Sign In   
  • Create Account


mrjfalk

Member Since 13 May 2009
Offline Last Active Jun 01 2012 02:48 PM

Topics I've Started

Quite simple C++ Class question

13 May 2009 - 11:44 PM

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 have tried reading the tutorial on GameDev.net, which was really nice, but still get this error. I get the following error:
Quote:
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
Quote:
#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
Quote:
#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
Quote:
#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

PARTNERS