我试图在堆类中仅为DijkstraState类创建专门的成员函数,但是当我编译并运行时,我得到错误,我不知道我是否专注于addElement,reheapdown,siftup以错误的方式
这是我的代码
#ifndef HEAP_H
#define HEAP_H
#include "DijkstraState.h"
template <class T> class Heap
{
private:
T* container; //Points to the array in which heap is implemented
int counter; //Number of full places
public:
Heap(int size=100)
{
container = new T[size];
counter = 0;
}
~Heap()
{
delete[] container;
}
bool isEmpty()
{
return (counter == 0) ;
}
void reHeapDown(int i);
void siftUp(int i);
void addElement(T x);
T& removeMin();
T& getMin();
};
template <class T> void Heap<T>::siftUp(int i)
{
while(true)
{
int parent = (i-1)/2 ;
if(parent < 0 )
{
break ;
}
if(container[i] < container[parent]) //element is smaller than its parent then swap
{
T temp = container[parent] ;
container[parent] = container[i] ;
container[i] = temp ;
i = parent ;
//set the index of the parent
}
else
{
break ;
}
}
}
template <>
void Heap<DijkstraState<class T>>::siftUp(int i)
{
while(true)
{
int parent = (i-1)/2 ;
if(parent < 0 )
{
break ;
}
if(container[i] < container[parent]) //element is smaller than its parent then swap
{
DijkstraState<T> temp = container[parent] ;
temp.setIndex(i) ;
container[parent] = container[i] ;
container[parent].setIndex(parent) ;
container[i] = temp ;
i = parent ;
//set the index of the parent
}
else
{
break ;
}
}
}
template <class T> void Heap<T>::reHeapDown(int i)
{
int smallestChild;
while(true)
{
int child1 = (2*i) + 1 ;
int child2 = (2*i) + 2 ;
if(child1 >= counter) // an element with no children
{
break;
}
if(child2 >= counter) //element has only one child
{
smallestChild = child1 ;
}
else if(container[child1] <= container[child2]) //first child is smaller than or equal second child
{
smallestChild = child1 ;
}
else
{
smallestChild = child2 ;
}
if(container[i] < container[smallestChild]) //parent is greater than or equal smallest child.
{
break ;
}
T temp = container[smallestChild] ;
container[smallestChild] = container[i] ;
container[i] = temp ;
i = smallestChild ;
//set the index of the child and parent
}
}
template <>
void Heap<DijkstraState<class T>>::reHeapDown(int i)
{
int smallestChild;
while(true)
{
int child1 = (2*i) + 1 ;
int child2 = (2*i) + 2 ;
if(child1 >= counter) // an element with no children
{
break;
}
if(child2 >= counter) //element has only one child
{
smallestChild = child1 ;
}
else if(container[child1] <= container[child2]) //first child is smaller than or equal second child
{
smallestChild = child1 ;
}
else
{
smallestChild = child2 ;
}
if(container[i] < container[smallestChild]) //parent is greater than or equal smallest child.
{
break ;
}
DijkstraState<T> temp = container[smallestChild] ;
temp.setIndex(i) ;
container[smallestChild] = container[i] ;
container[smallestChild].setIndex(smallestChild) ;
container[i] = temp ;
i = smallestChild ;
//set the index of the child and parent
}
}
template <class T> void Heap<T>::addElement(T x)
{
container[counter] = x ; //put the element in the last place in the array
int i = counter ; //index of the added element
counter++ ; //increase number of occupied places
if(counter == 1) //special case: empty heap
{
//set index of the newly added element
return;
}
siftUp(i);
}
template <>
void Heap<DijkstraState <class T>>::addElement(DijkstraState<T> x)
{
container[counter] = x ; //put the element in the last place in the array
int i = counter ; //index of the added element
container[counter].setIndex(i);
counter++ ; //increase number of occupied places
if(counter == 1) //special case: empty heap
{
//set index of the newly added element
return;
}
siftUp(i);
}
template <class T> T& Heap<T>::removeMin()
{
if(isEmpty())
{
return -1;
}
else
{
T min = container [0] ; //get the first element
container[0] = container[--counter] ; //set the last element to the first element and decrease counter by one
reHeapDown(0) ;
return min ;
}
}
template <class T> T& Heap<T>::getMin()
{
return container[0];
}
#endif
这些是生成的错误
1> main.cpp
DijkstraState.h(12): error C2079: 'DijkstraState<T>::d' uses undefined class 'T'
with
1> [
1> T=T
1> ]
1> c:\users\fakhr el din\documents\visual studio 2010\projects\dijkstra's_algorithm\dijkstra's_algorithm\Heap.h(68) : see reference to class template instantiation 'DijkstraState<T>' being compiled
1> with
1> [
1> T=T
1> ]
答案 0 :(得分:1)
你的问题是该类是模板化的(即,成员函数不是)因此你不能专门化特殊类所需的函数。但是,您可以对其进行配置,使得成员函数是根据模板化函数(或其他类似技术,允许您专门化实现的一个子集)实现的,此时您可以完全专门化或超载该功能。以下代码可能不是您所需要的,但说明了这一点。它使用独立函数和私有嵌套类(可以使用要修改的类状态进行实例化)。
#include <iostream>
template<typename T>
void bar()
{
std::cout << "general Foo::bar()\n";
}
template<>
void bar<int>()
{
std::cout << "specialized Foo::bar()\n";
}
template<typename U>
struct Bar2Impl
{
static void bar2() { std::cout << "general Foo::bar2()\n"; }
};
template<>
struct Bar2Impl<int>
{
static void bar2() { std::cout << "specialized Foo::bar2()\n"; }
};
template<typename T>
class Foo
{
private:
public:
void bar() { ::bar<T>(); }
void bar2() { Bar2Impl<T>::bar2(); }
};
int main()
{
Foo<double> f1;
f1.bar();
f1.bar2();
Foo<int> f2;
f2.bar();
f2.bar2();
return 0;
}
general Foo::bar()
general Foo::bar2()
specialized Foo::bar()
specialized Foo::bar2()