Jump to content
  • Advertisement
Sign in to follow this  
justcallmedrago

Abstract classes =D =D [FIXED!!]

This topic is 3902 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

I tried to incorporate animations into my DirectX program by following the gamedev article "Working with the DirectX .X File Format and Animation in DirectX 9.0". I included the header files (downloaded from the article):
#include "CModel.h"
#include "Macros.h"


and put them into the resource folder, etc. when I try to run it, i get the following errors:
Quote:
cmodel.cpp(45) : error C2259: 'CAllocateHierarchy' : cannot instantiate abstract class due to following members: 'HRESULT ID3DXAllocateHierarchy::CreateFrame(LPCSTR,LPD3DXFRAME *)' : is abstract d3dx9anim.h(134) : see declaration of 'ID3DXAllocateHierarchy::CreateFrame' 'HRESULT ID3DXAllocateHierarchy::CreateMeshContainer(LPCSTR,const D3DXMESHDATA *,const D3DXMATERIAL *,const D3DXEFFECTINSTANCE *,DWORD,const DWORD *,LPD3DXSKININFO,LPD3DXMESHCONTAINER *)' : is abstract d3dx9anim.h(168) : see declaration of 'ID3DXAllocateHierarchy::CreateMeshContainer' cmodel.cpp(64) : error C2259: 'CAllocateHierarchy' : cannot instantiate abstract class due to following members: 'HRESULT ID3DXAllocateHierarchy::CreateFrame(LPCSTR,LPD3DXFRAME *)' : is abstract d3dx9anim.h(134) : see declaration of 'ID3DXAllocateHierarchy::CreateFrame' 'HRESULT ID3DXAllocateHierarchy::CreateMeshContainer(LPCSTR,const D3DXMESHDATA *,const D3DXMATERIAL *,const D3DXEFFECTINSTANCE *,DWORD,const DWORD *,LPD3DXSKININFO,LPD3DXMESHCONTAINER *)' : is abstract d3dx9anim.h(168) : see declaration of 'ID3DXAllocateHierarchy::CreateMeshContainer' CAllocateHierarchy.cpp callocatehierarchy.cpp(20) : error C2511: 'HRESULT CAllocateHierarchy::CreateFrame(LPCSTR,LPD3DXFRAME *)' : overloaded member function not found in 'CAllocateHierarchy' callocatehierarchy.h(24) : see declaration of 'CAllocateHierarchy'
CModel::~CModel()
{
	//Delete Animation Controller
	SAFE_RELEASE(m_pAnimController)
	
	//if there is a frame hierarchyo
	if(m_pFrameRoot)
	{
		//Allocation class
		CAllocateHierarchy Alloc; //HAS ISSUES WITH THIS LINE (45)
		D3DXFrameDestroy(m_pFrameRoot, &Alloc);
		m_pFrameRoot = NULL;
	}

	//Delete the bones
	SAFE_DELETE_ARRAY(m_pBoneMatrices)

	//Make the Device not point to the other device
	m_pd3dDevice = NULL;
}


CAllocateHierarchy Define:
class CAllocateHierarchy: public ID3DXAllocateHierarchy
{
public:

	// Create a frame
    //1. The name of the frame
	//2. The output new frame
	STDMETHOD(CreateFrame)(THIS_ LPCTSTR Name, LPD3DXFRAME *ppNewFrame);
    
	// Create a Mesh Container
	//1. Name of the Mesh
	//2. The mesh Data
	//3. that materials of the mesh
	//4. the effects on the mesh
	//5. the number of meterials in the mesh
	//6. the adjacency array for the mesh
	//7. the skin information for the mesh
	//8. the output mesh container
	STDMETHOD(CreateMeshContainer)(THIS_ LPCTSTR Name, 
		LPD3DXMESHDATA pMeshData, LPD3DXMATERIAL pMaterials, 
		LPD3DXEFFECTINSTANCE pEffectInstances, DWORD NumMaterials, 
		DWORD *pAdjacency, LPD3DXSKININFO pSkinInfo, LPD3DXMESHCONTAINER *ppNewMeshContainer);
    
	// Destroy a frame
	//1. The frame to delete
	STDMETHOD(DestroyFrame)(THIS_ LPD3DXFRAME pFrameToFree);
    
	// Destroy a mesh container
	//1. The container to destroy
	STDMETHOD(DestroyMeshContainer)(THIS_ LPD3DXMESHCONTAINER pMeshContainerBase);
};

The code for "CreateFrame()":
HRESULT CAllocateHierarchy::CreateFrame(LPCSTR Name, LPD3DXFRAME *ppNewFrame)
{    
	// Create a frame
	// Using my drived struct here
	LPFRAME pFrame = new FRAME;
	ZeroMemory(pFrame, sizeof(FRAME));

	// Inicilize the passed in frame
	*ppNewFrame = NULL;

	// Put the name in the frame
	if(Name)
	{
	//	int nNameSize = strlen(Name)+1;
	//	pFrame->Name = new char[nNameSize];
	//	memcpy(pFrame->Name, Name, nNameSize*sizeof(char));
		pFrame->Name = NULL; //else
	}
		
	
	// Inicilize the rest of the frame
	pFrame->pFrameFirstChild = NULL;
	pFrame->pFrameSibling = NULL;
	pFrame->pMeshContainer = NULL;
	D3DXMatrixIdentity(&pFrame->matCombined);
	D3DXMatrixIdentity(&pFrame->TransformationMatrix);

	// Set the output frame to the one that we have
	*ppNewFrame = (LPD3DXFRAME)pFrame;

	// It no longer points to the frame
	pFrame = NULL;

	// Returns an HRESULT so give it the AOk result
    return S_OK;
}


Please help. I don't know what I'm doing, although I did read a bunch of articles on making derived classes <:( [Edited by - justcallmedrago on January 14, 2008 12:28:09 AM]

Share this post


Link to post
Share on other sites
Advertisement
I strongly suggest you read up on abstract classes, because there's a number of reasons that this could be happening, and it's important to know them.

There's something called a "pure virtual" function which means any class inheriting the abstract class must declare and define said function. Pure virtual functions cannot be used on their own, they must have their own definition created by any subclass using them, like I say.

Attempting to create an instance of such a class containing such a function will generate the "cannot instantiate abstract class" error you're seeing. You're even being told in the error what's causing the error (see the bit that says "due to following members"?)

However, reading that article, I can't see anything you're doing wrong. Have you followed the instructions to the letter, and/or have you changed anything or attempted to provide your own implementations? I'm going to assume the example code is supposed to compile without a hitch.

IMO you're in over your head if you looked at this without having a solid understanding of language basics. With all due respect you might want to work your way up to this sort of stuff, I made the same mistake as you did by getting too advanced too soon and it actually held me back big time.

Share this post


Link to post
Share on other sites
Quote:
However, reading that article, I can't see anything you're doing wrong. Have you followed the instructions to the letter, and/or have you changed anything or attempted to provide your own implementations? I'm going to assume the example code is supposed to compile without a hitch.


I did read up on abstract classes, since gamedev was out for hours and I couldn't post a question. I haven't made my own abstract/derived class yet though :/ .

Yes this code is supposed to be compiled without a hitch, but it seems it uses an older version of directX because it used a function "SetTime()" which didn't exist.

I did edit that function out, but the "cannot instantiate abstract class" error was there from the beginning.

Quote:
IMO you're in over your head if you looked at this without having a solid understanding of language basics. With all due respect you might want to work your way up to this sort of stuff, I made the same mistake as you did by getting too advanced too soon and it actually held me back big time.


You're right about this code being in over my head, but I hope to be able to understand this later but implement it now, and that error is the only problem.
Can anyone work with me to fix it?

[Edited by - justcallmedrago on January 13, 2008 8:42:27 PM]

Share this post


Link to post
Share on other sites
Are you compiling with Unicode? If you are, you will get compilation errors because the author of that code you're using specified the "Name" parameters of CreateFrame and CreateMeshContainer as type "LPCTSTR". And as you may or may not know, LPCTSTR resolves to "const wchar_t*" when Unicode is flagged. Those parameters should instead be type "LPCSTR", since that's what the original definition of ID3DXAllocateHierarchy specifies. If you mismatch parameters like that, you'll get an error saying you're incorrectly overloading a function and you won't be able to instantiate the class due to pure virtual members that haven't actually had an implementation written for them.

Share this post


Link to post
Share on other sites
Quote:
Original post by MJP
Are you compiling with Unicode? If you are, you will get compilation errors because the author of that code you're using specified the "Name" parameters of CreateFrame and CreateMeshContainer as type "LPCTSTR". And as you may or may not know, LPCTSTR resolves to "const wchar_t*" when Unicode is flagged. Those parameters should instead be type "LPCSTR", since that's what the original definition of ID3DXAllocateHierarchy specifies. If you mismatch parameters like that, you'll get an error saying you're incorrectly overloading a function and you won't be able to instantiate the class due to pure virtual members that haven't actually had an implementation written for them.


EUREAKA!

With that information, I was able to get it to compile!!!!! That was exactly the problem! For "CreateMeshContainer" the parameters were wrong, such as "LPD3DXMESHDATA pMeshData" instead of "CONST D3DXMESHDATA *pMeshData". I copied the param list from D3DX9anim.h and it was fixed just like that!


I can't express my gratitude in words.

That's... 2? 3 times you've gotten me excellent info?!

you are amazing.

Share this post


Link to post
Share on other sites
Well then drago, that's it for the freebies. Next time I expect generous donations to my PayPal account! [smile]

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!