Skip to main content
GameDev.net gamedev.net
🔒 Locked

inheritance

Started by Crispy Dec 14, 2002 at 6:31 AM 3 replies 200+ views
Original Post
Crispy
Crispy
Hi, why will this give me a "''TPDLight:osition'' is not an unambiguous base class of ''TProjectileBase''" error?
  
class TPDLight
{
   protected:
   	TVector3f Position;
   ...
};

class TProjectileBase : public TPDLight
{
   private:
      TVector3f Orientation;

      float Speed;

   public:
   	TProjectileBase(TVector3f pPosition, TVector3f pOrientation, float pSpeed)
      	: Orientation(pOrientation), Speed(pSpeed), TPDLight::Position(pPosition) {}; //<- here


   ...
};
  
I''m not accessing TPDLight:osition as a class, but rather as a member, right? Crispy
"Literally, it means that Bob is everything you can think of, but not dead; i.e., Bob is a purple-spotted, yellow-striped bumblebee/dragon/pterodactyl hybrid with a voracious addiction to Twix candy bars, but not dead."- kSquared
Stary
Stary
quote:
Original post by Crispy
Hi,
why will this give me a "'TPDLight:osition' is not an unambiguous base class of 'TProjectileBase' " error?


Because you're calling a member of a different class in the initialization list for your constructor, which makes the compiler think that it's a constructor call for a base class. Since the initialization list is run "before" the constructor itself, there's no guarantee that TPDLight:osition even exists yet.

One possible solution to this is to put the initialization of Position inside the constructor itself, which would mean that Position would be constructed twice. Another solution is to create a constructor for the base class:


      
class TPDLight
{
public:
TPDLight(const TVector3f &pPosition) : Position(pPosition) {} // <- new constructor

protected:
TVector3f Position;
...
};

class TProjectileBase : public TPDLight
{
private:
TVector3f Orientation;

float Speed;

public:
TProjectileBase(TVector3f pPosition, TVector3f pOrientation, float pSpeed)
: TPDLight(pPosition), Orientation(pOrientation), Speed(pSpeed) {}; // <- call the constructor instead of the protected inherited member


...
};


Edit: If you wonder, the const TVector3f & instead of TVector3f is to avoid copying the TVector3fs all the time. You should probably consider it for other non-copying calls as well.

[edited by - Stary on December 14, 2002 7:55:39 AM]
joanusdmentia
joanusdmentia
you can''t initialize a base classes members from the initializer list of the constructor, it has to be done by the constructor of the base class itself. you can do this by


  
class TPDLight
{
protected:
TVector3f Position;
public:
TPDLight(TVector3f &pPosition) : Position(pPosition) {}
};

class TProjectileBase : public TPDLight
{
private:
TVector3f Orientation;
float Speed;
public:
TProjectileBase(TVector3f &pPosition, TVector3f &pOrientation, float pSpeed)
: Orientation(pOrientation), Speed(pSpeed), TPDLight(pPosition) {}
};
"Voilà! In view, a humble vaudevillian veteran, cast vicariously as both victim and villain by the vicissitudes of Fate. This visage, no mere veneer of vanity, is a vestige of the vox populi, now vacant, vanished. However, this valorous visitation of a bygone vexation stands vivified, and has vowed to vanquish these venal and virulent vermin vanguarding vice and vouchsafing the violently vicious and voracious violation of volition. The nly verdict is vengeance; a vendett o
joanusdmentia
joanusdmentia
damn it!!! beat me!!
"Voilà! In view, a humble vaudevillian veteran, cast vicariously as both victim and villain by the vicissitudes of Fate. This visage, no mere veneer of vanity, is a vestige of the vox populi, now vacant, vanished. However, this valorous visitation of a bygone vexation stands vivified, and has vowed to vanquish these venal and virulent vermin vanguarding vice and vouchsafing the violently vicious and voracious violation of volition. The nly verdict is vengeance; a vendett o
Crispy
Crispy
Heck. This is precisely what I was thinking of avoiding. I figured that the derivee (fancy for base class ) would be created right after the derived class, allowing the members to be access, totally ignoring the fact that this what constructors are for - to control the creation flow.

Thanks guys,
Crispy
"Literally, it means that Bob is everything you can think of, but not dead; i.e., Bob is a purple-spotted, yellow-striped bumblebee/dragon/pterodactyl hybrid with a voracious addiction to Twix candy bars, but not dead."- kSquared

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.