Jump to content

Sharing data between classes?

- - - - -

  • Please log in to reply
6 replies to this topic

#1
sacredheart

sacredheart

    Newbie

  • Members
  • Pip
  • 7 posts
Hi there,
I am trying to tackle a (probably fundamentally easy :) problem.

How do I fetch data from one function in class1 to another in class2?

I have my declarations.h file in which I declare my stuff:

class Player {

private:

	int PosY;

	int PosX;


public:

	Player() {

		PosY = 8;

		PosX = 13;

	};


	int GetCoords(int i);

	Player* player;


};


class DrawMap {

private:


public:

	DrawMap() {

	};

	

	int showMap();

	DrawMap* myMap;


};

In my functions.cpp file I include the declarations.h file, and define my class functions:

#include "declarations.h"


int Player::GetCoords(int i) {

	if(i == 1) {

		return PosX;

	} else {

		return PosY;

	}

}


int DrawMap::showMap() {

	int PosX = player->GetCoords(1); <-- This is wrong, but why and how?

	return 0;

}

The compiler says
error: ‘player’ was not declared in this scope
which I totally understand. DrawMap:: doesn't know what player is. But how do I get it to know that? Shouldn't Player* player be public and accessible?

TIA,
A

#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others
DrawMap doesn't have any data about player, either as a method or class variable, so from player's perspective, it simply doesn't exist. Similarly, Player* player means each INSTANCE of Player has a pointer to a Player, but that doesn't mean you've created any instances of Player yet.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#3
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,718 posts
  • Programming Language:C, Java, C++, PHP, Python, Perl, Assembly, Bash, Others
  • Learning:JavaScript
Edit: I swear WP's answer wasn't there when I started writing. :D

That would be because you haven't declared a Player object named player in showMap nor DrawMap. It's kinda like declaring a variable X in one function and expecting another function to be able to access it. Unless it's public and static, or if you pass a pointer/reference to DrawMap, you won't be able to access it.
sudo rm -rf /

#4
sacredheart

sacredheart

    Newbie

  • Members
  • Pip
  • 7 posts
Thanks for the swift reply!

I forgot to mention, I do have
Player player(10.0, 10);
in main() -- which creates the player object (doesn't it?).

Should I create the object somewhere else than in main()?

BR,
A

#5
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,118 posts
  • Location:Vancouver, Eh! Cleverness: 200
Could you show us your full code for the file of which contains main?
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#6
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others
Unfortunately, DrawMap doesn't know about variables you instantiate in main() unless they are passed to its methods as parameters.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#7
Zer033

Zer033

    Learning Programmer

  • Members
  • PipPipPip
  • 79 posts
You could have the drawmap class construct with a player variable being passed to it to fill an internal player object it has, or make a method that receives a player object and stores info of it in an internal player object, or make showmap take a player object argument and work with it there, or you could declare the player you plan to work on as public static and access it even when an object isn't created using the scope resolution operator
 :: 
, if done like that each instance of Player will share that "working" player variable. You don't really need pointers to your classes within those classes you can call their functions within that class without them. Also if you're going to be passing parameters of objects by value or returning class objects or constructing using another class and those classes have pointer data members then you should create your own copy constructors so there won't be shallow copy and create destructors.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users