How to fix "error LNK2019" in implementation of heap?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Despo
    New Member
    • Jan 2011
    • 2

    How to fix "error LNK2019" in implementation of heap?

    I have this program which is an implementation of a heap and the error LNK2019 occurs

    Code:
    #include <iostream>
    
    using namespace std;
    
    
    class Heap {
    public:
    	int treeData[1000]; // a heap of up to 999 elements. position 0 is kept for size
    	void heap();
    	void add(int);
    };
    
    void Heap::heap() {
    	treeData[0] = 0; //set the size to 0
    }
    
    void Heap::add(int v) {
    	treeData[0]++; //increase the size
    	int pos = treeData[0];
    	treeData[pos] = v; //put the value in the next position in the complete tree
    	while(pos >1) { //as long as the root is not reached
    		if(treeData[pos] > treeData[pos/2]) {//if value of node is bigger than the parent
    			int tmp = treeData[pos];
    			treeData[pos] = treeData[pos/2];
    			treeData[pos/2] = tmp; //swap the value of the child with the parent
    			pos = pos/2; //update the position to the parent
    		}
    		else return; //else stop
    	}
    }
    
    void Heap::remove(){
        int size= treeData[0]; 
        treeData[1]=treeData[size];
        treeData[size]= 0; 
        treeData[0] --;
        
        for (int i=1; i<(treeData[0]/2)+1;i++){ 
            if (treeData[i]<treeData[i*2]){ 
    			int tmp =treeData[i];    
    			treeData[i]=treeData[i*2];
    			treeData[i*2]=tmp;
    		}
        
            if(treeData[i]<treeData[(i*2)+1]){
    			int tmp=treeData[i];
    			treeData[i]=treeData[(i*2)+1];
    			treeData[(i*2)+1]=tmp;
    	    }
    	}
    }
    
    void Heap::showroot() { 
        int root;
        root=treeData[1];
        cout << root <<endl;
    }
    
    
    
    void main() {
    	Heap myheap;
    	myheap.add(35);
    	myheap.add(68);
    	myheap.add(76);
    	myheap.add(22);
    	myheap.add(9);
    	myheap.add(83);
    	myheap.add(12);
    	myheap.add(2);
    	myheap.add(43);
    	cout << myheap.treeData[0];
    
    
         for (int i=myheap.treeData[0]; i !=0, --i;) { 
                        cout << num++ <<":  ";
                        myheap.showroot();
                        myheap.remove();
         }
    
    
    }
    //What should i do???????
    Last edited by Niheel; Jan 19 '11, 07:54 AM. Reason: please use code tags
  • tdlr
    New Member
    • Jan 2011
    • 22

    #2
    You need to declare the Heap::remove and Heap::showRoot methods inside the class' declaration:

    Code:
    class Heap {
    public:
    int treeData[1000]; // a heap of up to 999 elements. position 0 is kept for size
    Heap();
    void add(int);
    void remove();
    void showRoot();
    };
    Also I think the constructor name should be case sensitive, so you should change the name of the constructor to Heap. Also, the constructor has no return value!

    Comment

    • Despo
      New Member
      • Jan 2011
      • 2

      #3
      Thanks for the help... I'll try it!!! =)

      Comment

      Working...