Dynamic Cast Errors

This is a discussion on Dynamic Cast Errors within the C++ Programming forums, part of the General Programming Boards category; I recieve this error when casting: C:/Mike/Uni/215/A2/tree.cpp:214: cannot dynamic_cast ` currentNode->Node::getData()' (of type `<unknown type>') to type `class NumericTreeItem*' (source ...

  1. #1
    Registered User
    Join Date
    Jan 2003
    Posts
    36

    Dynamic Cast Errors

    I recieve this error when casting:

    C:/Mike/Uni/215/A2/tree.cpp:214: cannot dynamic_cast `
    currentNode->Node::getData()' (of type `<unknown type>') to type `class
    NumericTreeItem*' (source is not a pointer)

    TreeItem*' (source is not a pointer)

    yet from my examples I see this is how I should be casting

    Code:
    class TreeItem	//an object for a node
    {
    
    	public:
    		virtual string stringFormat()=0;
    };
    
    class StringTreeItem: public TreeItem
    {
    	private:
    		string data;
    
    	public:
    		StringTreeItem(string item)
    		{
    			data = item;
    		}
    		string stringFormat()
    		{
    			return data;//already a string
    		};
    };
    
    class NumericTreeItem: public TreeItem
    {
    
    	public:
    		virtual string stringFormat()=0;
    		virtual double number()=0;
    };
    
    class IntTreeItem: public NumericTreeItem
    {
    	private:
    		int data;
    	public:
    		IntTreeItem(int number)
    		{
    			data = number;
    		}
    		string stringFormat()
    		{
    			ostrstream S; //an output stringstream
    			S << data; //just print to the stringstream!
    			S.put(0); //need to null-terminate the string
    			//now extract the string from the stringstream
    			string aString = S.str(); //and you can use aString as you want...
    
    			return aString;
    
    		}
    		double number()
    		{
    			return (double)data;
    		}
    
    };
    
    class FloatTreeItem: public NumericTreeItem
    {
    	private:
    		double data;
    
    	public:
    		FloatTreeItem(double number)
    		{
    			data=number;
    		}
    		string stringFormat()
    		{
    			ostrstream S; //an output stringstream
    			S << data; //just print to the stringstream!
    			S.put(0); //need to null-terminate the string
    			//now extract the string from the stringstream
    			string aString = S.str(); //and you can use aString as you want...
    
    			return aString;
    		}
    		double number()
    		{
    			return data;
    		}
    
    };
    
    class Node
    {
    	private:
    		TreeItem *item;
    		Node* left;
    		Node* right;
    	public:
    		Node(TreeItem* tree)
    		{
    			item = tree;
    			left=NULL;
    			right=NULL;
    		}
    		Node* getLeft()
    		{
    			return left;
    		}
    		Node* getRight()
    		{
    			return right;
    		}
    		void setLeft(Node* left)
    		{
    			this->left =left;
    		}
    		void setRight(Node* right)
    		{
    			this->right = right;
    		}
    		TreeItem* getData()
    		{
    			return item;
    		}
    		void print()
    		{
    
    		}
    };
    
    	double getSum(double sum,Node* currentNode)
    	{
    		//prints entry entry subtree subtree subtree
    		NumericTreeItem* data = dynamic_cast<NumericTreeItem*>(currentNode->getData);
    
    		if(data != NULL)		//successful cast
    		{
    			//cast to int or double
    			IntTreeItem* item = dynamic_cast<IntTreeItem*>(data);
    			if(item !=NULL)	//then an int
    			{
    				sum = sum+(double)(item->number());
    			}
    			else
    			{
    				FloatTreeItem* decimal = dynamic_cast<FloatTreeItem*>(data);
    				if(decimal!=NULL)
    					sum = sum + (item->number());
    			}
    		}
    
    
    		if(currentNode->getLeft()!=NULL)
    		{
    
    			getSum(sum,currentNode->getLeft());
    		}
    
    		if(currentNode->getRight()!=NULL)
    		{
    			getSum(sum,currentNode->getRight());
    		}
    
    		return sum;
    	}
    I've included classes and methods involved, although they are in inline order for the file.

    Why would I be recieivng that error, since I am casting a "known" Tree Item - I've tried using tree Item but get a similar error.

  2. #2
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348
    Add parentheses to getData.

    getData()

    Kuphryn

  3. #3
    Registered User
    Join Date
    Jan 2003
    Posts
    36
    it always has to be the silly errors that make you feel foolish
    :P
    thx

  4. #4
    Registered User
    Join Date
    Jan 2003
    Posts
    36
    with a dynamic cast, does it return anything?

    as in what should I be checking for to make sure it's the proper NumericTreeItem and not a StringTreeItem?

    if(data!=NULL)?

    if(data!=0)?

    Either way I get a general protection fault(I feel like I'm writing windows95 :P)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Header File Errors...
    By Junior89 in forum C++ Programming
    Replies: 5
    Last Post: 07-08-2007, 01:28 AM
  2. Linker errors in VC++ 2005
    By C+/- in forum C++ Programming
    Replies: 0
    Last Post: 05-18-2007, 08:42 AM
  3. Including The Right DLLs
    By bumfluff in forum Game Programming
    Replies: 8
    Last Post: 12-28-2006, 03:32 AM
  4. executing errors
    By s0ul2squeeze in forum C++ Programming
    Replies: 3
    Last Post: 03-26-2002, 01:43 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21