[英]How to specialize template member function for another template class?
我試圖在堆類中僅對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> ]
您的問題是該類是模板化的(即,沒有成員函數),因此您無法對需要特殊化該類的功能進行特殊化。 但是,您可能可以對其進行配置,以便根據模板化函數(或其他類似技術,可以專門化實現的子集)來實現成員函數,此時,您可以完全專門化或重載功能。 以下代碼可能不是您所需要的,但可以說明這一點。 它使用一個獨立的函數和一個私有的嵌套類(可以用要修改的類狀態實例化)。
#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()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.