C
CHAFIK Wassime
hi
well i was testing various implementations of sorting algorithms,
and i found my self stuck in this error
testSortingCpp.cpp:36: undefined reference to `Sorting<Node>::fibHeapSort
(std::vector<Node*, std::allocator<Node*> >&)'
i'm using g++ ver: 4.3.3 on a i486-linux-gnu platform
#####################################################
here is sorting.hpp
#####################################################
*!implementation of various sorting algorithms.
*/
#ifndef __SORTING_HPP
#define __SORTING_HPP
#include <vector>
/**utility class providing various sorting algorithms and helper methods.
*/
template<typename T>
class Sorting{
public:
static void fibSiftDown( std::vector<T*>& heap,int start ,int end);
/**<sifting down upon a fibonacci heap stored in a vector*/
static void fibHeapify( std::vector<T*>& heap,int end);
/**<heapify a fibonacci heap stored in a vector*/
static void fibHeapSort( std::vector<T*>& heap);
/**< the actual fibonacci heap sort*/
};
#endif
##################################################
here is sorting.cpp
##################################################
#include "sorting.hpp"
using namespace std;
template<typename T>
void Sorting<T>::fibSiftDown(vector<T*>& heap,int start,int end){
int firstChild=3*start+1;
int maxChild = firstChild;
//if start hasn't at least a child return
if(firstChild>end) return;
//point to the max child
if(firstChild+1<=end && *heap[firstChild+1] > *heap[firstChild] ){
maxChild++;
}
if(firstChild+2<=end && *heap[firstChild+2] > *heap[maxChild] ){
maxChild=firstChild+2;
}
if(*heap[start] < *heap[maxChild]){
T* tmp = heap[start];
heap[start] = heap[maxChild];
heap[maxChild] = tmp;
//siftdown the child too
fibSiftDown(heap,maxChild,end);
}
};
template<typename T>
void Sorting<T>::fibHeapify(vector<T*>& heap,int end){
int start = (end-2)/3;
while(start>=0){
fibSiftDown(heap,start,end);
start--;
}
};
template<typename T>
void Sorting<T>::fibHeapSort(vector<T*>& heap){
int end = heap.size()-1;
while(end>0){
fibHeapify(heap,end);
T* tmp = heap[0];
heap[0] = heap[end];
heap[end] = tmp;
end--;
}
}
#########################################################
finally the testing main,
#########################################################
#include "sorting.hpp"
#include <iostream>
#include <vector>
#include <stdlib.h>
#include <time.h>
#define MAXELEMENT 100
using namespace std;
class Node{
public:
int val;
Node(int a):val(a){};
Node(const Node& n):val(n.val){};
};
bool operator < (const Node a,const Node b){
return a.val < b.val;
};
bool operator > (const Node a,const Node b){
return a.val > b.val;
};
bool operator <= (const Node a,const Node b){
return a.val<=b.val;
};
bool operator >= (const Node a,const Node b){
return a.val>=b.val;
};
bool operator == (const Node a,const Node b){
return a.val == b.val;
}
int main(int arg, char** args){
srand(time(0));
vector<Node *> * ptree = new vector<Node*>(MAXELEMENT,new Node(rand
()));
for(int i =1;i<MAXELEMENT;i++){
(*ptree)= new Node(rand());
};
Sorting<Node>::fibHeapSort(*ptree);
cout<<"ordered sequence"<<endl;
for(int i = 0;i<MAXELEMENT;i++){
cout<<(*ptree)->val<<endl;
};
return 0;
}
############################################
i really can't understand what happened exactly
i tested various flavor of this implementation but i can't understand
this particular one
sorry for my English and thanks in advance
well i was testing various implementations of sorting algorithms,
and i found my self stuck in this error
testSortingCpp.cpp:36: undefined reference to `Sorting<Node>::fibHeapSort
(std::vector<Node*, std::allocator<Node*> >&)'
i'm using g++ ver: 4.3.3 on a i486-linux-gnu platform
#####################################################
here is sorting.hpp
#####################################################
*!implementation of various sorting algorithms.
*/
#ifndef __SORTING_HPP
#define __SORTING_HPP
#include <vector>
/**utility class providing various sorting algorithms and helper methods.
*/
template<typename T>
class Sorting{
public:
static void fibSiftDown( std::vector<T*>& heap,int start ,int end);
/**<sifting down upon a fibonacci heap stored in a vector*/
static void fibHeapify( std::vector<T*>& heap,int end);
/**<heapify a fibonacci heap stored in a vector*/
static void fibHeapSort( std::vector<T*>& heap);
/**< the actual fibonacci heap sort*/
};
#endif
##################################################
here is sorting.cpp
##################################################
#include "sorting.hpp"
using namespace std;
template<typename T>
void Sorting<T>::fibSiftDown(vector<T*>& heap,int start,int end){
int firstChild=3*start+1;
int maxChild = firstChild;
//if start hasn't at least a child return
if(firstChild>end) return;
//point to the max child
if(firstChild+1<=end && *heap[firstChild+1] > *heap[firstChild] ){
maxChild++;
}
if(firstChild+2<=end && *heap[firstChild+2] > *heap[maxChild] ){
maxChild=firstChild+2;
}
if(*heap[start] < *heap[maxChild]){
T* tmp = heap[start];
heap[start] = heap[maxChild];
heap[maxChild] = tmp;
//siftdown the child too
fibSiftDown(heap,maxChild,end);
}
};
template<typename T>
void Sorting<T>::fibHeapify(vector<T*>& heap,int end){
int start = (end-2)/3;
while(start>=0){
fibSiftDown(heap,start,end);
start--;
}
};
template<typename T>
void Sorting<T>::fibHeapSort(vector<T*>& heap){
int end = heap.size()-1;
while(end>0){
fibHeapify(heap,end);
T* tmp = heap[0];
heap[0] = heap[end];
heap[end] = tmp;
end--;
}
}
#########################################################
finally the testing main,
#########################################################
#include "sorting.hpp"
#include <iostream>
#include <vector>
#include <stdlib.h>
#include <time.h>
#define MAXELEMENT 100
using namespace std;
class Node{
public:
int val;
Node(int a):val(a){};
Node(const Node& n):val(n.val){};
};
bool operator < (const Node a,const Node b){
return a.val < b.val;
};
bool operator > (const Node a,const Node b){
return a.val > b.val;
};
bool operator <= (const Node a,const Node b){
return a.val<=b.val;
};
bool operator >= (const Node a,const Node b){
return a.val>=b.val;
};
bool operator == (const Node a,const Node b){
return a.val == b.val;
}
int main(int arg, char** args){
srand(time(0));
vector<Node *> * ptree = new vector<Node*>(MAXELEMENT,new Node(rand
()));
for(int i =1;i<MAXELEMENT;i++){
(*ptree)= new Node(rand());
};
Sorting<Node>::fibHeapSort(*ptree);
cout<<"ordered sequence"<<endl;
for(int i = 0;i<MAXELEMENT;i++){
cout<<(*ptree)->val<<endl;
};
return 0;
}
############################################
i really can't understand what happened exactly
i tested various flavor of this implementation but i can't understand
this particular one
sorry for my English and thanks in advance