Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.
Moderator: Coders of Rage
GroundUpEngine
Chaos Rift Devotee
Posts: 835 Joined: Sun Nov 08, 2009 2:01 pm
Current Project: mixture
Favorite Gaming Platforms: PC
Programming Language of Choice: C++
Location: UK
Post
by GroundUpEngine » Tue Apr 13, 2010 9:31 pm
I swear I've done this before and it worked, wtf.. or maybe i'm just tired
test.cpp
Code: Select all
#include <iostream>
using namespace std;
class Entity {
public:
Entity();
~Entity();
};
class Actor : public Entity {
public:
Actor() : Entity() {}
~Actor() {}
};
class Player : public Actor {
public:
Player() : Actor() {}
~Player() {}
int testVar;
void Shoot() {};
};
int main()
{
Actor* test = new Player();
test->testVar = 0;
test->Shoot();
cin.get();
delete test;
return 0;
}
Error: (16) 'class Actor' has no member named 'testVar'
Error: (17) 'class Actor' has no member named 'Shoot'
Purpose: I wanna use it in this function, so I can return a generic Actor or a Player
Code: Select all
ActorManager TestManager;
Actor* TestActor;
TestActor = TestManager.GetPlayer("TestName");
---
Actor* ActorManager::GetPlayer(const string &name)
{
// Check if the Player is not Already Loaded
map<string, Actor*>::iterator iter = Actors.find(name);
if((Actors.size() < MAX_ACTOR_COUNT) && (iter == Actors.end()))
{
Actor* act = new Player();
act->name = name;
Actors[name] = act;
cout << name << " is new player\n";
}
return Actors[name];
}
Last edited by
GroundUpEngine on Wed Apr 14, 2010 10:47 am, edited 1 time in total.
XianForce
Chaos Rift Devotee
Posts: 767 Joined: Wed Oct 29, 2008 8:36 pm
Post
by XianForce » Tue Apr 13, 2010 10:45 pm
well, testVar and Shoot() aren't in the Actor class. They are in the player class.
Because you have a pointer to an Actor, you can only use anything an Actor has.
So to solve, either chain up the variable/function or make virtual functions.
GroundUpEngine
Chaos Rift Devotee
Posts: 835 Joined: Sun Nov 08, 2009 2:01 pm
Current Project: mixture
Favorite Gaming Platforms: PC
Programming Language of Choice: C++
Location: UK
Post
by GroundUpEngine » Wed Apr 14, 2010 8:58 am
XianForce wrote: well, testVar and Shoot() aren't in the Actor class. They are in the player class.
Because you have a pointer to an Actor, you can only use anything an Actor has.
So to solve, either chain up the variable/function or make virtual functions.
Thanks, ye I was looking for an alternative but maybe I'll just use some virtual functions in the base class instead.
RyanPridgeon
Chaos Rift Maniac
Posts: 447 Joined: Sun Sep 21, 2008 1:34 pm
Current Project: "Triangle"
Favorite Gaming Platforms: PC
Programming Language of Choice: C/C++
Location: UK
Contact:
Post
by RyanPridgeon » Wed Apr 14, 2010 10:18 am
You could put it in a Player pointer temporarily to call the function
Code: Select all
int main()
{
Actor* test = new Player();
test->testVar = 0;
Player* temp = test;
temp->Shoot();
cin.get();
delete test;
return 0;
}
Ryan Pridgeon
C ,
C++ ,
C# ,
Java ,
ActionScript 3 ,
HaXe ,
PHP ,
VB.Net ,
Pascal
Music | Blog
Falco Girgis
Elysian Shadows Team
Posts: 10294 Joined: Thu May 20, 2004 2:04 pm
Current Project: Elysian Shadows
Favorite Gaming Platforms: Dreamcast, SNES, NES
Programming Language of Choice: C/++
Location: Studio Vorbis, AL
Contact:
Post
by Falco Girgis » Wed Apr 14, 2010 10:23 am
You could also just as easily cast it. XD
RyanPridgeon
Chaos Rift Maniac
Posts: 447 Joined: Sun Sep 21, 2008 1:34 pm
Current Project: "Triangle"
Favorite Gaming Platforms: PC
Programming Language of Choice: C/C++
Location: UK
Contact:
Post
by RyanPridgeon » Wed Apr 14, 2010 10:39 am
GyroVorbis wrote: You could also just as easily cast it. XD
Oh yeah, d'oh
Ryan Pridgeon
C ,
C++ ,
C# ,
Java ,
ActionScript 3 ,
HaXe ,
PHP ,
VB.Net ,
Pascal
Music | Blog
GroundUpEngine
Chaos Rift Devotee
Posts: 835 Joined: Sun Nov 08, 2009 2:01 pm
Current Project: mixture
Favorite Gaming Platforms: PC
Programming Language of Choice: C++
Location: UK
Post
by GroundUpEngine » Wed Apr 14, 2010 10:46 am
RyanPridgeon wrote: GyroVorbis wrote: You could also just as easily cast it. XD
Oh yeah, d'oh
My thoughts exactly, Thanks guys!