Sign in to follow this  

Inheritance question

This topic is 4531 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, I have a base class Object with name, id, a dispose method etc. But when I Inherit it I can't see any of those methods and it won't let me compile saying method doesn't exist. I am using c++ vs2003 and directx. For example my Input class doesn't see the object methods. This is just some psuedocode. Is there any reason why something wouldn't be inherited?

class Object
{
public:
 getName();
}

class Input : public Object
{
 //Whatever in here
}

Input i;

i.getName(); //Error


Share this post


Link to post
Share on other sites
I didn't want to post all my code because that would be alot to look through. It doesn't come up in Intellisense. I think it would come up there. The inheritance itself doesn't give a compiler error. All I wanted to know is there any reason why something wouldn't be inherited? Not just in my code but any code?

Share this post


Link to post
Share on other sites
It could be declared private in the base class, or you could have not used public inheritence in the derived class.

Share this post


Link to post
Share on other sites
I think i have gotten closer to the error. I have namespaces, so will that cause an inheritance error.

Example


namespace BaseClass
{
class Object
{
public:
getName();
}
}

namespace InputClass
{
class Input : public BaseClass::Object
{
//Whatever in here
}
}

Input i;

i.getName(); //Error





EDIT:: I can use the methods from the base class if it's in the same namespace, but if I inherit in a different namespace i can't. Is there something wrong in my code, or do I need to reference the namespace somehow I thought I was with the class Input : public BaseClass::Object.

Share this post


Link to post
Share on other sites
Im out of ideas then, how can I see the methods if they are in the same namespace but if its not I can't see the methods. I'm getting ready to go to a different language.

Share this post


Link to post
Share on other sites
Zip your code or post your class design. You only need the 2 classes.

The base and the inherited. Then show us the implementation.

Its ok to have a lot of code posted.

Share this post


Link to post
Share on other sites
this code works fine, and they are in different namespaces

#include <iostream>

namespace One{
class Test{
public:
int getOne(){
return 550;
}
};

}


namespace Two{

class TestTwo : public One::Test{

};

}



int main(){
Two::TestTwo obj;
std::cout << obj.getOne() << std::endl;
}

Share this post


Link to post
Share on other sites

Base class object



//===============================================================================================
//
// File :xObject.h
//
// Date Created : 6/30/2005
// Date Modified : 7/13/2005
//
//===============================================================================================
//
// The base class for all objects
//
//===============================================================================================

#ifndef _XOBJECT_H_
#define _XOBJECT_H_

#include "Impulse.h"

namespace Impulse
{

namespace Base
{

class xObject
{

/* PRIVATE MEMBER VARIABLES */
private:

int m_iUniqueID;
std::string m_strName;

/* PROTECTED MEMBER VARIABLES */
protected:

bool m_bDisposed;

/* PUBLIC METHODS */
public:

/* CONSTRUCTORS */
xObject(void);
xObject(std::string name);

/* DESTRUCTOR */
~xObject(void);

//Calls the public dispose method
void Dispose(void);

//Gets the unique ID of the object
int UniqueID(void);

//Gets the name of the object
std::string Name(void);

/* PROTECTED METHODS */
protected:

//Inheritable method, release any allocated memory
virtual void Dispose(bool disposing);

}; //End class xObject

} //End namespace Base

} //End namespace Impulse

#endif




Scene Node class in same namespace, I can use the methods here

//===============================================================================================
//
// File : xSceneNode.h
//
// Date Created : 7/01/2005
// Date Modified : 7/13/2005
//
//===============================================================================================
//
// This is the base class for all things that are renderable to the screen.
//
//===============================================================================================

#ifndef _XSCENENODE_H_
#define _XSCENENODE_H_

#include "Impulse.h"

namespace Impulse
{

namespace Base
{
//class xSceneNode inherits from xObject
class xSceneNode : public Impulse::Base::xObject
{

/* PRIVATE MEMBER VARIABLES */
private:

IDirect3DDevice9 *m_pD3DDevice;
D3DXVECTOR3 m_v3Translation,
m_v3Rotation,
m_v3Origin,
m_v3Scale;
D3DXMATRIX m_matWorld,
m_matTranslation,
m_matRotation,
m_matScale;
bool bIsVisible;

/* PUBLIC METHODS */
public:

/* CONSTRUCTORS */
xSceneNode(IDirect3DDevice9 *pD3DDevice);
xSceneNode(IDirect3DDevice9 *pD3DDevice, std::string name);

//Gets the nodes render device
IDirect3DDevice9 *GetDevice();

// setdevice method
void SetDevice(IDirect3DDevice9 *pD3DDevice);

//This updates the world matrix
void Update(void);

//Returns each of the matrices, WORLD, TRANSLATION, ROTATION, SCALE
D3DXMATRIX GetWorldMatrix(void);
D3DXMATRIX GetTranslationMatrix(void);
D3DXMATRIX GetRotationMatrix(void);
D3DXMATRIX GetScaleMatrix(void);

//Returns each of the vectors, TRANSLATION, ROTATION, SCALE
D3DXVECTOR3 GetTranslationVector3(void);
D3DXVECTOR3 GetRotationVector3(void);
D3DXVECTOR3 GetScaleVector3(void);

//Sets the absolute translation of the node
void SetAbsoluteTranslation(D3DXVECTOR3 v);
void SetAbsoluteTranslation(float x, float y, float z);

//Sets the abosulte rotation of the node
void SetAbsoluteRotation(D3DXVECTOR3 v);
void SetAbsoluteRotation(float x, float y, float z);

//Sets the absoulte origin of the node
void SetAbsoluteOrigin(D3DXVECTOR3 v);
void SetAbsoluteOrigin(float x, float y, float z);

//Sets the absolute scale of the node
void SetAbsoluteScale(D3DXVECTOR3 v);
void SetAbsoluteScale(float x, float y, float z);

//Sets the relative translation of the node
void SetRelativeTranslation(D3DXVECTOR3 v);
void SetRelativeTranslation(float x, float y, float z);

//Sets the relative rotation of the node
void SetRelativeRotation(D3DXVECTOR3 v);
void SetRelativeRotation(float x, float y, float z);

//Sets the relative origin of the node
void SetRelativeOrigin(D3DXVECTOR3 v);
void SetRelativeOrigin(float x, float y, float z);

//Sets the relative scale of the node
void SetRelativeScale(D3DXVECTOR3 v);
void SetRelativeScale(float x, float y, float z);

//Inheritable render method
virtual bool Render(void);

//Turns on the bounding box for rendering
void DebugData(bool debug);

//Gets the position of the bounding box
virtual D3DXVECTOR3 GetPosition(void);

//Gets the dimension of the bounding box
virtual D3DXVECTOR3 GetDimension(void);

/* PROTECTED METHODS */
protected:

//Inheritable method, called when a node is updated
virtual void OnUpdate(void);

//This calculates the points after translation, rotation, scale
//The child class of this class passes in all the points in
//the OnUpdate inherited method
virtual void CalculatePoints(D3DXVECTOR3 *original, D3DXVECTOR3 *points, int length);

//Bounding Box Information
bool m_bRenderVolume;
xCustomVertex::PositionColored m_VerticesColored[4];
D3DXVECTOR3 m_v3Position, m_v3Dimension;
D3DXMATRIX m_matBBWorld, m_matBBTranslation, m_matBBScale, m_matBBRotation;
IDirect3DVertexBuffer9 *m_pVB;
IDirect3DIndexBuffer9 *m_pIB;
short m_sIndices[8];
D3DCOLOR m_PointColors[4];

//Sets up the indices for the bounding box
virtual void SetupBoundingBoxIndices(void);

//Sets up the vertices for the bounding box
virtual void SetupBoundingBoxVertices(void);

//Sets up the bounding box position and dimension
virtual void SetupBoundingBox(D3DXVECTOR3 position, D3DXVECTOR3 dimension);
virtual void SetupBoundingBox(float x, float y, float z,
float width, float height, float depth);

//Renders the bounding box
virtual void RenderBoundingBox(void);

//Calculates the points of the bounding box
virtual void CalculateBoundingBoxFromPoints(D3DXVECTOR3 *points, int length);

}; //End class xSceneNode

} //End namespace Base

} //End namespace Impulse

#endif



Input class cannot see methods

//===============================================================================================
//
// File : xInputDevice.h
//
// Date Created : 7/14/2005
// Date Modified : 7/14/2005
//
//===============================================================================================
//
// Handles input from the keyboard or mouse
//
//===============================================================================================

#ifndef _XINPUTDEVICE_H_
#define _XINPUTDEVICE_H_

#include "Impulse.h"

using namespace Impulse::Base;

namespace Impulse
{

namespace Input
{

enum DeviceType
{
DT_KEYBOARD = 0,
DT_MOUSE = 1
};

class xInputDevice : public Impulse::Base::xObject
{

/* PRIVATE MEMBER VARIABLES */
private:

IDirectInput8 *m_pDI; //DirectInput Object
IDirectInputDevice8 *m_pDIDevice; //DirectInput Device
HWND m_hWnd; //Window Handle
unsigned char m_State[256]; //State of keyboard
DIMOUSESTATE *m_MouseState; //State of mouse
long m_XPos, m_YPos; //Position of mouse
bool m_Windowed; //Windowed or not
bool m_Clicked; //If mouse was clicked
DeviceType m_deviceType; //Device type
long m_DeviceSize; //Device Size
DIDATAFORMAT *m_DataFormat; //Data format of the Device

/* PRIVATE METHODS */

//Create a direct input device
bool CreateInputDevice(void);

//Initializes the keyboard device type
bool InitializeKeyboard(void);

//Initializes the mouse device type
bool InitializeMouse(void);

/* PUBLIC METHODS */

public:

/* CONSTRUCTOR */
xInputDevice(HWND hWnd, IDirectInput8 *pDI, bool Windowed = true,
DeviceType type = DT_KEYBOARD);

//Clears out the device
void Clear(void);

//Reads the input device
bool Read(void);

//Gets the key state of a key press
bool KeyState(unsigned long Key);

//Gets the button state of a mouse press
bool GetButtonState(unsigned long Button);

//Returns if the mouse has been clicked
bool OnClick(void);

//Gets the position of the mouse
long GetXPos(void);
long GetYPos(void);
long GetXDelta(void);
long GetYDelta(void);

/* PROTECTED METHODS */
protected:

//Inherits from xObject, releases memory
virtual void Dispose(bool disposing);

};


} //End Input Namespace

} //End Impulse Namespace

#endif

Share this post


Link to post
Share on other sites
Quote:

using namespace Impulse::Base;

namespace Impulse
{

namespace Input
{


this is the offending region. The compiler thinks You are using Impulse::Base::Impulse::Input. Remove "using namespace Impulse::Base" and you should be fine. If you really want to keep it, put it after the class declaration, but I wouldn't recomended it in a header file.

Share this post


Link to post
Share on other sites
c:\Documents and Settings\abc\My Documents\MyProjects\DirectX\C++\ImpulseTetris\WinMain.cpp(274): error C2660: 'Impulse::Sprites::xLabel::Dispose' : function does not take 0 arguments
c:\Documents and Settings\abc\My Documents\MyProjects\DirectX\C++\ImpulseTetris\WinMain.cpp(273): error C2660: 'Impulse::Sprites::xLabel::Dispose' : function does not take 0 arguments
c:\Documents and Settings\abc\My Documents\MyProjects\DirectX\C++\ImpulseTetris\WinMain.cpp(272): error C2660: 'Impulse::Sprites::xLabel::Dispose' : function does not take 0 arguments
c:\Documents and Settings\abc\My Documents\MyProjects\DirectX\C++\ImpulseTetris\WinMain.cpp(271): error C2660: 'Impulse::Sprites::xLabel::Dispose' : function does not take 0 arguments
c:\Documents and Settings\abc\My Documents\MyProjects\DirectX\C++\ImpulseTetris\WinMain.cpp(289): error C2660: 'Impulse::Input::xInputDevice::Dispose' : function does not take 0 arguments
c:\Documents and Settings\abc\My Documents\MyProjects\DirectX\C++\ImpulseTetris\WinMain.cpp(287): error C2660: 'Impulse::Managers::xDeviceManager::Dispose' : function does not take 0 arguments
c:\Documents and Settings\abc\My Documents\MyProjects\DirectX\C++\ImpulseTetris\WinMain.cpp(290): error C2660: 'Impulse::Managers::xInputManager::Dispose' : function does not take 0 arguments
c:\Documents and Settings\abc\My Documents\MyProjects\DirectX\C++\ImpulseTetris\WinMain.cpp(243): error C2660: 'Impulse::Managers::xSceneManager::Dispose' : function does not take 0 arguments
c:\Documents and Settings\abc\My Documents\MyProjects\DirectX\C++\ImpulseTetris\WinMain.cpp(245): error C2660: 'Impulse::Primitives::xQuad::Dispose' : function does not take 0 arguments
c:\Documents and Settings\abc\My Documents\MyProjects\DirectX\C++\ImpulseTetris\WinMain.cpp(246): error C2660: 'Impulse::Primitives::xQuad::Dispose' : function does not take 0 arguments
c:\Documents and Settings\abc\My Documents\MyProjects\DirectX\C++\ImpulseTetris\WinMain.cpp(247): error C2660: 'Impulse::Primitives::xQuad::Dispose' : function does not take 0 arguments
c:\Documents and Settings\abc\My Documents\MyProjects\DirectX\C++\ImpulseTetris\WinMain.cpp(248): error C2660: 'Impulse::Primitives::xQuad::Dispose' : function does not take 0 arguments
c:\Documents and Settings\abc\My Documents\MyProjects\DirectX\C++\ImpulseTetris\WinMain.cpp(249): error C2660: 'Impulse::Primitives::xQuad::Dispose' : function does not take 0 arguments
c:\Documents and Settings\abc\My Documents\MyProjects\DirectX\C++\ImpulseTetris\WinMain.cpp(250): error C2660: 'Impulse::Primitives::xQuad::Dispose' : function does not take 0 arguments
c:\Documents and Settings\abc\My Documents\MyProjects\DirectX\C++\ImpulseTetris\WinMain.cpp(251): error C2660: 'Impulse::Primitives::xQuad::Dispose' : function does not take 0 arguments
c:\Documents and Settings\abc\My Documents\MyProjects\DirectX\C++\ImpulseTetris\WinMain.cpp(252): error C2660: 'Impulse::Primitives::xQuad::Dispose' : function does not take 0 arguments
c:\Documents and Settings\abc\My Documents\MyProjects\DirectX\C++\ImpulseTetris\WinMain.cpp(253): error C2660: 'Impulse::Primitives::xQuad::Dispose' : function does not take 0 arguments
c:\Documents and Settings\abc\My Documents\MyProjects\DirectX\C++\ImpulseTetris\WinMain.cpp(254): error C2660: 'Impulse::Primitives::xQuad::Dispose' : function does not take 0 arguments
c:\Documents and Settings\abc\My Documents\MyProjects\DirectX\C++\ImpulseTetris\Block.cpp(180): error C2660: 'Impulse::Primitives::xQuad::Dispose' : function does not take 0 arguments

Share this post


Link to post
Share on other sites
What's happening is pretty simple, actually. The virtual dispose function is trumping the inherited dispose function during overload resolution. Because the virtual function is present right in the class, it always wins, even when the parameters aren't right.

You could change the name of the non-virtual function; Or if you are only calling it from derived functions you could call it using Base::Dispose().

Share this post


Link to post
Share on other sites
Thanks a lot that was it. why can't I see the other methods, like GetName called Name, and GetId call UniqueID in intellisense, thats what was throwing me off was stupid intellisense.

Well I got the shutdowns working releasing the memory when the window is closed, but that didn't solve my original issue, it errors out saying memory can;t be read from so and so place, probably because I still have something still active. But thats for another day.

Share this post


Link to post
Share on other sites
You can't see them in intellisense because the intellisense is not smart enough. The only way to know for sure what a class would have in all but the most basic cases is too compile the code, and intellisense isn't doing that. It just parses it and does the best it can. If it's not sure about something, it leaves it out. Better to not have it at all then have it wrong. DevCpp's intellisense is somewhat better than VS, and works more consistently, but VS does all the parsing on the fly and, if you've installed Devcpp, you are familiar with the time it takes it to build the intellisense database the first time around.

Share this post


Link to post
Share on other sites

This topic is 4531 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this