CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Dec 2010
    Posts
    907

    Can't instantiate absstract class

    Sorry, two more questions

    Code:
     #ifndef PERFECTSIM_PARSER
    #define PERFECTSIM_PARSER
    
     
     
    
    #include <string>
    #include <d3dx9.h>
    #include <sstream>
    #include <iostream>
    
    #include "tinyxml\tinyxml.h"
    
    
    using namespace std;
    
    template<class T>
    class GetValue
    {
    protected:
    	virtual T get(TiXmlNode* pParent);
    	virtual T get_attribs(TiXmlNode* pParent) = 0;
    };
    
     
     
    class GetVector3 : public GetValue<D3DXVECTOR3>
    {
    public:
    	virtual D3DXVECTOR3 get(TiXmlNode* pParent);
    	virtual D3DXVECTOR3 get_attribs(TiXmlElement *element);
    	D3DXVECTOR3 get_vector3(TiXmlElement *element);
    	
    };
    
    template<class T>
    T GetValue<T>::get(TiXmlNode* pParent)
    {
    	T attr;
    	if ( !pParent ) return attr;
    
    	TiXmlNode* pChild;
    	TiXmlText* pText;
    	int t = pParent->Type();
    	
    	 
     
    
    	switch ( t )
    	{
    	case TiXmlNode::TINYXML_DOCUMENT:
    	 
    		break;
    
    	case TiXmlNode::TINYXML_ELEMENT:
    	 
    		 attr =get_attribs(pParent->ToElement());
    		break;
    
    	case TiXmlNode::TINYXML_COMMENT:
    	 
    		break;
    
    	case TiXmlNode::TINYXML_UNKNOWN:
    		 
    		break;
    
    	case TiXmlNode::TINYXML_TEXT:
    		pText = pParent->ToText();
    		 
    		break;
    
    	case TiXmlNode::TINYXML_DECLARATION:
    		 
    		break;
    	default:
    		break;
    	}
    	 
    	for ( pChild = pParent->FirstChild(); pChild != 0; pChild = pChild->NextSibling()) 
    	{
    		get( pChild );
    	}
    
    	return attr;
    
    }
    
     
    #endif
    Code:
    // tutorial demo program
    #include "stdafx.h"
    #include "Parser.h"
    
     
    /////////////////////////////////////
    ///
    
    D3DXVECTOR3 GetVector3::get(TiXmlNode* pNode)
    {
    	return GetValue::get(pNode);
    }
    
     
    D3DXVECTOR3 GetVector3::get_vector3(TiXmlElement* pElement)
    {
    
    	return get_attribs(pElement);
    	 
    }
    
    D3DXVECTOR3 GetVector3::get_attribs(TiXmlElement* pElement)
    {
    	
    	if ( !pElement ) return D3DXVECTOR3();
    
    	GetValue::get(pElement);
    	D3DXVECTOR3 v;
    
    	pElement->QueryFloatAttribute("X", &v.x);
    	pElement->QueryFloatAttribute("Y", &v.y);
    	pElement->QueryFloatAttribute("Z", &v.z);
    
    	return v;
    
    
    }
    1) Can't instantiate abstract class of GetVector3.
    2) Don't you think the coding is very redundant ?

    Any help please?
    Thanks
    Jack

  2. #2
    Join Date
    May 2009
    Posts
    2,413

    Re: Can't instantiate absstract class

    Quote Originally Posted by lucky6969b View Post
    1) Can't instantiate abstract class of GetVector3.
    Just make sure all virtual methods have concrete implementations.

  3. #3
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,042

    Re: Can't instantiate absstract class

    Quote Originally Posted by lucky6969b View Post
    2) Don't you think the coding is very redundant ?
    The design seems very complicated. It's not clear to me what you want to achieve. Why do you have a template base class? Why does it have only protected virtual functions?

    You should first think about what you want your program to do. Then think about a (class) design that can fulfill those requirements and only then you should start coding.
    Cheers, D Drmmr

    Please put [code][/code] tags around your code to preserve indentation and make it more readable.

    As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky

  4. #4
    Join Date
    Aug 2002
    Location
    Madrid
    Posts
    4,588

    Re: Can't instantiate absstract class

    When you overwrite a pure virtual function, it has to have the same signature. Otherwise you haven't actually overwritten it, you just have declared an unrelated virtual function.
    Code:
    virtual T get_attribs(TiXmlNode* pParent) = 0;
    ...
    D3DXVECTOR3 GetVector3::get_attribs(TiXmlElement* pElement)
    So the base class's get_attribs(TiXmlNode* pParent) function is still not defined in the derived class GetVector3 and hence the compiler says that the class contains undefined virtual functions and is an abstract class.
    Get this small utility to do basic syntax highlighting in vBulletin forums (like Codeguru) easily.
    Supports C++ and VB out of the box, but can be configured for other languages.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured