Hello, everyone!
When creating an object of a class, how do I create a pointer to that object so that multiple other classes can access the same object (in this case: the other classes are inheriting from the class that contains the object). Code and scenario can be supplied upon request if this is needed (I would do now, but I don't know if it is or is not possible without this).
Thanks.
That doesn't sound like a situation where a pointer is warranted.
If the base class contains an object, it is guaranteed to exist for the entire lifetime of the derived class, therefore the derived class doesn't need to check if the pointed object exists, therefore, it's sufficient to use a reference.
A reference is what I meant. Sorry for my lack of specification, though - I am fairly new to this (and quite tired), so I occasionally make mistakes in naming what I want to do. Yes, I do know the difference between an object and a class (again, bad wording).
Scenario:
I am making a (console based) RPG. I wish to have a reference in the base class to the map object because if there is just a declaration of the map object, every derived class will have it's own map.
Code:
The code can be found at the following link (there are several other errors in the code (I only learned that developers plan code after I had started and had to restructure the game half way through, as well as underestimating the project's complexity, so I had to make some changes (that are yet to be finished (once I can deal with this and the problem just to be mentioned))), namely being due to the LivingObject class not recognizing that GameObject is a class. I think that it is due to a circular dependency, but I may be wrong. If anyone else sees what the problem is, could you please tell me? I can deal with the rest of the problems.): http://www.4shared.com/zip/7kmznpvE/Text_Based_RPG_1.html
Thanks everyone (sorry if this is a bit vague, I can try to explain better if this makes too little sense to be of any use).
I tried to implement this inside the class (initializing the pointer to the object in the constructor, so it was done correctly) and all of the previous error messages disappeared (to be replaced with a really long one that I couldn't decipher (extremely long and complex)). I then moved the map object (and the relevant pointer to the object to the global namespace) and got 6 errors, stating: "error: request for member 'xxxxxxx' in 'MpoP', which is of non-class type 'MapFunctions*'" ('xxxxxxx' being a function). Could anybody please correct this for me?
codekiddy, didn't you post your own .rar file for someone to download in the C++ forum? I'm not sure what the difference is between someone else posting a hosted file and you posting a hosted file, but try not to rag on someone for doing the exact same thing you did...
Hi Kazekan,
Yes you're completly right but that's different, the thing which I uploaded was something that can't be represented using code tags, so the only way was to upload it.
#ifndef MAPFUNCTIONS_H_INCLUDED
#define MAPFUNCTIONS_H_INCLUDED
#include <iostream>
#include <cstdlib>
#include <new>
#include <string>
#include <sstream>
#include <fstream>
#include "Enemy.h"
usingnamespace std;
class MapFunctions
{
public:
MapFunctions();
void MapRead(); // Get a map from a file.
int ReadMap(int xvalue, int yvalue); // Read one part of the map.
void WriteMap(int xvalue, int yvalue, int writevalue); // Write a new value to one part of the map.
void MapDelete();
int getLevel();
int getRows();
int getCols();
void setLevel();
~MapFunctions();
protected:
int level;
private:
int ** Map;
int rows, cols, StartTextSize;
string StartText;
};
#endif // MAPFUNCTIONS_H_INCLUDED
#include "GameObject.h"
GameObject::GameObject()
{
//ctor
}
//Works out the directions in which the player may travel.
void GameObject::DirectionCheck(int tlocx, int tlocy)
{
bool DirectionSpecifier[4];
int DirectionSpecifierCount = 0;
if(tlocy<Mpo.getRows())
{
if(Mpo.ReadMap(tlocx,tlocy+1)!=0)
{
DirectionSpecifier[North]=1;
DirectionSpecifierCount++;
}
}
else
{
DirectionSpecifier[North]=0;
}
if(tlocx<Mpo.getCols())
{
if(Mpo.ReadMap(tlocx+1,tlocy)!=0)
{
DirectionSpecifier[East]=1;
DirectionSpecifierCount++;
}
}
else
{
DirectionSpecifier[East]=0;
}
if(tlocy>0)
{
if(Mpo.ReadMap(tlocx,tlocy-1)!=0)
{
DirectionSpecifier[South]=1;
DirectionSpecifierCount++;
}
}
else
{
DirectionSpecifier[South]=0;
}
if(tlocx>0)
{
if(Mpo.ReadMap(tlocx-1,tlocy)!=0)
{
DirectionSpecifier[West]=1;
DirectionSpecifierCount++;
}
}
else
{
DirectionSpecifier[West]=0;
}
for(int i=0; i<4; i++)
{
if(DirectionSpecifier[i]!=0 && DirectionSpecifierCount>2)
{
switch(i)
{
case 0:
cout << "north, ";
break;
case 1:
cout << "east, ";
break;
case 2:
cout << "south, ";
break;
case 3:
cout << "west, ";
break;
}
DirectionSpecifierCount--;
}
elseif(DirectionSpecifier[i]!=0 && DirectionSpecifierCount>1)
{
switch(i)
{
case 0:
cout << "north and ";
break;
case 1:
cout << "east and ";
break;
case 2:
cout << "south and ";
break;
case 3:
cout << "west and ";
break;
}
DirectionSpecifierCount--;
}
elseif(DirectionSpecifierCount=1 && DirectionSpecifier[i]!=0)
{
switch(i)
{
case 0:
cout << "north.";
break;
case 1:
cout << "east.";
break;
case 2:
cout << "south.";
break;
case 3:
cout << "west.";
break;
}
DirectionSpecifierCount--;
}
}
}
GameObject::~GameObject()
{
//dtor
}
I know that this program's structure is a bit confused: I only found out that programmers plan code after starting and I have nearly restructured the program to a plan, but I need to ensure that each relevant class can get to the same map object first before doing it.nI also underestimated the project's complexity when starting it.
Is this enough information? If not, I can post more code.