Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Inheritance problems, how to access sub class functions?? (general c++)  (Read 4687 times)

0 Members and 1 Guest are viewing this topic.

HailBee

  • Newbie
  • *
  • Posts: 25
    • View Profile
    • Email
Ok so the idea is to have a NPC class that has all the variables to work from, other NPC's will inherient from it. Inherited NPC's then get placed into a 'NPC' vector.

ie:

 class NPC
 {
public:
   sf::Sprite npcSprite;
   sf::Sprite& getNPC() {return npcSprite;};
   kf::Vector2f moveDir; //Movement Direction towards player
   sf::Vector2f startPos; //Starting position
   float moveSpeed; //Movement velocity
   bool alive; //Check if alive/dead
   float getRotation(sf::RenderWindow &win, sf::Sprite &npc, sf::Sprite &player);
};

class blueNPC : public NPC
{
public:
   blueNPC();
   void move(float deltaT, sf::Sprite &player, sf::RenderWindow &win);
private:
   sf::Texture blueNPC_tex;
   float scale; //Animate by resizing sprite on x axis
   bool goWide; //set sprite X scale outwards
};


BUT

My problem is how do I access blueNPC's move function within the vector as 'npc' doesnt have a move function ? ie:


npcVec[1].move(deltaT, player, win);   //as 'npc' does not have a move function

And my vector was setup like this:

for(int i = 0; i < maxNPC; i++)
   {
      npcVec.push_back(blueNPC());
      npcVec.push_back(purpleNPC());
      npcVec.push_back(greenNPC());
      npcVec.push_back(snakeNPC());
   }   

//Also im aware I could make a vector of each kind of NPC, I'd just rather not as it seems really un-intuitive.
« Last Edit: September 16, 2013, 02:40:51 pm by HailBee »

Geheim

  • Full Member
  • ***
  • Posts: 201
    • View Profile
    • Email
Re: Inheritance problems, how to access sub class functions?? (general c++)
« Reply #1 on: September 16, 2013, 02:40:52 pm »
make a virtual function in the NPC class thats exactly like the one in your blueNPC class:
virtual void move(float deltaT, sf::Sprite &player, sf::RenderWindow &win) = 0;
Failing to succeed does not mean failing to progress!

HailBee

  • Newbie
  • *
  • Posts: 25
    • View Profile
    • Email
Re: Inheritance problems, how to access sub class functions?? (general c++)
« Reply #2 on: September 16, 2013, 02:41:53 pm »
Awesome I knew I tuned out at Uni when we were learning virtual's but had a inkling that it had something to do with that, will get on it now!

Thanks again! :D

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Inheritance problems, how to access sub class functions?? (general c++)
« Reply #3 on: September 16, 2013, 02:46:24 pm »
If you store copies instead of pointers in your array, you will get the problem known as "slicing". I let you Google for it :P
Laurent Gomila - SFML developer

HailBee

  • Newbie
  • *
  • Posts: 25
    • View Profile
    • Email
Re: Inheritance problems, how to access sub class functions?? (general c++)
« Reply #4 on: September 16, 2013, 03:04:09 pm »
Haha guessing Slicing = hacky memory but yea ill look into it :)

Also if you can point me in the right direction with this error thats now occuring:


1>  collision.cpp   //and a few other cpp files
1>c:\program files (x86)\microsoft visual studio 10.0\vc\include\vector(869): error C2259: 'NPC' : cannot instantiate abstract class
1>          due to following members:
1>          'void NPC::move(float,sf::Sprite &,sf::RenderWindow &)' : is abstract
1>          c:\users\public\videos\geowars v2\geometry wars\dev\include\hayleigh\npcs.h(18) : see declaration of 'NPC::move'
1>          c:\users\public\videos\geowars v2\geometry wars\dev\include\hayleigh\npcmanager.h(18) : see reference to class template instantiation 'std::vector<_Ty>' being compiled
1>          with
1>          [
1>              _Ty=NPC
1>          ]

I'd be most thankful!

aka what does abstract mean in this situation so I can fix it ;) -- for reference each base class like in the code above takes the same amount of arguments as the virtual so im stumped..


Edit: Perhaps it has to do with me creating the vector like this(as it seems to refer to this in the error)     
std::vector<NPC> npcVec;   //Vector of NPC's
for(int i = 0; i < maxNPC; i++)
   {
      npcVec.push_back(blueNPC());
      npcVec.push_back(purpleNPC());
      npcVec.push_back(greenNPC());
      npcVec.push_back(snakeNPC());
   }   
« Last Edit: September 16, 2013, 03:30:24 pm by HailBee »

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Inheritance problems, how to access sub class functions?? (general c++)
« Reply #5 on: September 16, 2013, 03:25:21 pm »
Please read a good C++ book, these are really basics. The error message clearly states what's wrong -- but you lack the background knowledge.

And Laurent already told you not to store copies in the container.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

HailBee

  • Newbie
  • *
  • Posts: 25
    • View Profile
    • Email
Re: Inheritance problems, how to access sub class functions?? (general c++)
« Reply #6 on: September 16, 2013, 03:29:08 pm »
Please read a good C++ book, these are really basics. The error message clearly states what's wrong -- but you lack the background knowledge.

And Laurent already told you not to store copies in the container.

Well i've made, thermonuclear war, space invaders and almost finished Geometry Wars all in c++..

I've read books and im almost in my 2nd year at Uni in a programming degree so although I dont understand why my virtual function is abstract when it takes the same amount of arguments in inherited classes im not a total noob :P

I have read up on virtual class documentation online and am unable to find the problem considering the arguments it takes are all the same, if you have the knowledge to share some light on this I'd appreciate it...

edit: for instance http://stackoverflow.com/questions/4586744/why-is-the-following-code-giving-me-an-error-about-an-abstract-class does not apply here even though its a very similar problem.
« Last Edit: September 16, 2013, 03:33:27 pm by HailBee »

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Inheritance problems, how to access sub class functions?? (general c++)
« Reply #7 on: September 16, 2013, 03:32:46 pm »
Well i've made, thermonuclear war, space invaders and almost finished Geometry Wars..
Yes, but this doesn't express as much about the C++ knowledge as one might think.

im not a total noob :P
I'm not saying that, but you obviously have some gaps which you could easily fill when reading the chapters about classes, inheritance and polymorphism in a good C++ book. Of course we can help you for this specific problem, but a book can explain these things much better and won't leave you with the next question right ahead. Also, it will save you a lot of time ;)

I have read up on virtual class documentation online and am unable to find the problem, im guessing you can't either?
You should definitely stop guessing :P

Have a look at the following topics: Virtual functions, pure virtual functions, abstract classes, overriding.
« Last Edit: September 16, 2013, 03:36:34 pm by Nexus »
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

HailBee

  • Newbie
  • *
  • Posts: 25
    • View Profile
    • Email
Re: Inheritance problems, how to access sub class functions?? (general c++)
« Reply #8 on: September 16, 2013, 03:35:52 pm »
If anyone else cares to share some help knowing that I've looked over virtual docs including:

http://stackoverflow.com/questions/4586744/why-is-the-following-code-giving-me-an-error-about-an-abstract-class

and can provide some help or a link in the right direction I'd love to learn from you.
« Last Edit: September 16, 2013, 03:37:23 pm by HailBee »

Geheim

  • Full Member
  • ***
  • Posts: 201
    • View Profile
    • Email
Re: Inheritance problems, how to access sub class functions?? (general c++)
« Reply #9 on: September 16, 2013, 03:45:16 pm »
As two times already told you, don't use copies, use pointers!
std::vector<NPC*> npcVec;
Failing to succeed does not mean failing to progress!

HailBee

  • Newbie
  • *
  • Posts: 25
    • View Profile
    • Email
Re: Inheritance problems, how to access sub class functions?? (general c++)
« Reply #10 on: September 16, 2013, 04:12:47 pm »
Yay Third times the charm ;)

Silly me was madly looking into virtual functions and polymorphism and it was just an issue with having to pass pointers :P

I did hear yall say it thrice but I thought it was nitpicking rather than a solution(until that final comment where I got yelled at for good reason) my humble apology and thankyou!!

Kojay

  • Full Member
  • ***
  • Posts: 104
    • View Profile
Re: Inheritance problems, how to access sub class functions?? (general c++)
« Reply #11 on: September 16, 2013, 04:32:51 pm »
Silly me was madly looking into virtual functions and polymorphism and it was just an issue with having to pass pointers :P

No need to go beyond 'thank you'; a comment like this only leaves you looking like a fool.

HailBee

  • Newbie
  • *
  • Posts: 25
    • View Profile
    • Email
Re: Inheritance problems, how to access sub class functions?? (general c++)
« Reply #12 on: September 16, 2013, 05:13:39 pm »
Silly me was madly looking into virtual functions and polymorphism and it was just an issue with having to pass pointers :P

No need to go beyond 'thank you'; a comment like this only leaves you looking like a fool.

Harsh! I was reading what I was recommend to read regarding the issue in previous posts... :P

ps: I dig the EverQuest avatar(pretty sure thats it, hard to tell at that size lol)...but no need to be rude!