Pointer to template class error

This is a discussion on Pointer to template class error within the C++ Programming forums, part of the General Programming Boards category; I am getting a segmentation fault when I try to dereference the pointer and call a simple member function. I ...

  1. #1
    Registered User
    Join Date
    Jan 2007
    Posts
    40

    Pointer to template class error

    I am getting a segmentation fault when I try to dereference the pointer and call a simple member function. I don't understand though???

    Is there a special syntax when dereferencing pointers to template classes or something?

    heap.h
    Code:
    namespace assignment5
    {
    	#include <iostream>  
    	#include <cstdlib>  // Provides NULL and size_t
    	using namespace std;
    
        template <class Item, class Key>
        class heapnode
        {
    		public:
    			heapnode(const Item& init_data = Item(), Key init_key = Key(), heapnode* init_left = NULL, heapnode* init_right = NULL, heapnode* init_parent = NULL)
    			{
    				data_field	= init_data;
    				key_field	= init_key;
    				left_child	= init_left;
    				right_child	= init_right;
    				parent_node	= init_parent;
    			}
    			~heapnode()
    			{
    				if(left_child)
    					delete left_child;
    				if(right_child)
    					delete right_child;
    				if(parent_node)
    					delete parent_node;
    			}
    
    			Key			getKey() { return key_field; }
    			void		setKey(Key temp) { key_field = temp; }
    			Item		getItem() { return data_field; }
    			void		setItem(Item temp) { data_field = temp; }
    			heapnode*	getLeft() { return left_child; }
    			void		setLeft(heapnode* temp) { left_child = temp; }
    			heapnode*	getRight() { return right_child; }
    			void		setRight(heapnode* temp) { right_child = temp; }
    			heapnode*	getParent() { return parent_node; }
    			void		setParent(heapnode* temp) { parent_node = temp; }
    			
    			bool isLeaf() const
    			{
    				if(parent_node && !left_child && !right_child)
    					return true;
    				else
    					return false;
    			}
    			bool isRoot() const
    			{
    				if(!parent_node)
    					return true;
    				else
    					return false;
    			}
    		private:
    			Key			key_field;
    			Item		data_field;
    			heapnode*	left_child;
    			heapnode*	right_child;
    			heapnode*	parent_node;
        };
    
    	template <class Process, class Item, class Key>
    	void inorder_processing(heapnode<Item, Key> *root, Process f);
    	template <class Process, class Item, class Key>
    	void preorder_processing(heapnode<Item, Key> *root, Process f);
    	template <class Process, class Item, class Key>
    	void postorder_processing(heapnode<Item, Key> *root, Process f);
    	template <class Item, class Key>
    	bool check_node(heapnode<Item, Key>* root, Key k);
    	template <class Item, class Key>
    	void insert_node(heapnode<Item, Key>*& root, Key k, const Item& entry);
    	template <class Item, class Key>
    	void remove_node(heapnode<Item, Key>*& root, Key k);
    	template <class Process, class Param, class Item, class Key>
    	bool process_node(heapnode<Item, Key> *root, Key k, Process f, Param p);
    
    	#include "heap.cxx"
    }
    heap.cxx
    Code:
    #include <iostream>  // Provides cin and cout
    #include <cstdlib>   // Provides EXIT_SUCCESS
    #include "heap.h"
    using namespace std;
    using namespace assignment5;
    
    int main( )
    {
        heapnode<int, int> *mydb = NULL;
        
        cout << mydb->getParent() << endl;
    
        return EXIT_SUCCESS;
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    23,630
    Actually, NULL and size_t are defined in <cstddef>.

    As for the segmentation fault, it looks like you are trying to deference a null pointer in main().
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jan 2007
    Posts
    40
    ah. yes. you're right.

    I thought the problem lied in that line anyways...

    I also get a segfault when running this ->

    Code:
    template <class Item, class Key>
    void insert_node(heapnode<Item, Key>*& root, Key k, const Item& entry)
    {
    	cout << "insert node" << endl;
    	if(root->isRoot())
    		cout << "root!" << endl;
    }
    Code:
    int main( )
    {
        heapnode<int, int> *mydb = NULL;
        
        insert_node(mydb, 0, 100);
    
        return EXIT_SUCCESS;
    }

  4. #4
    Registered User
    Join Date
    Jan 2007
    Posts
    40
    Is my problem that I need to create a root node for mydb to point to? and then I can start dereferencing and doing heapification? The problem is just creating that first node if the root pointer is NULL?

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    23,630
    Now you are trying to dereference a null pointer in insert_node(). Look at the root->isRoot() call.

    Is my problem that I need to create a root node for mydb to point to? and then I can start dereferencing and doing heapification? The problem is just creating that first node if the root pointer is NULL?
    Well, insert_node() could be modified to check for a null pointer, in which case it creates the root node.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. An error is driving me nuts!
    By ulillillia in forum C Programming
    Replies: 5
    Last Post: 04-04-2009, 09:15 PM
  2. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 02:07 AM
  3. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  4. Errors including <windows.h>
    By jw232 in forum Windows Programming
    Replies: 4
    Last Post: 07-29-2008, 01:29 PM
  5. Couple C questions :)
    By Divx in forum C Programming
    Replies: 5
    Last Post: 01-28-2003, 12:10 AM

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