gpt4 book ai didi

List的c++排序函数

转载 作者:行者123 更新时间:2023-11-27 23:29:33 24 4
gpt4 key购买 nike

我的排序函数如下所示:

template <typename Compare, typename T>    
void List<T>::sort(const Compare& comparer){
...
}

但我收到下一个错误:

template definition of non-template void List<T>::sort(const Compare&)'<br/>
invalid use of undefined type
class List'

这是什么意思?

这是列表的完整代码:

template <typename T> class Node;    
template <typename T> class Iterator;
template <typename T>
class List{
private:
Node<T> * first;
Node<T> * last;
int size;
friend class Predicate ;
friend class Compare;
public:
typedef Iterator<T> iterator;

List(){
first = NULL;
last = NULL;
size = 0;
}
List(const List<T> & l);
void pushBack(T element);
void insert( const T& element, iterator i);
iterator remove(iterator i);
iterator find(const Predicate& predicate);
void sort(const Compare& comparer);
int getSize() const;
iterator begin();
iterator end();
~List();
};

template <class T>
List<T>::List(const List & l) {
first = 0;
last = 0;
size = 0;
for (Node<T> * current = l.first; current != 0; current = current -> next){
pushBack(current -> data);
}
}

template <typename T>
void List<T>::pushBack(T element){
Node<T>* newnode = new Node<T>(element);
if (newnode->prev == NULL) {
first = newnode;
last = newnode;
}else{
newnode->prev = last;
last->next = newnode;
last = newnode;
}
}

template <typename T>
void List<T>::insert( const T& element, iterator i){
if (i.position == NULL){
pushBack(element);
++size;
return;
}
Node<T>* after = i.position;
Node<T>* before = after->prev;
Node<T>* newnode = new Node<T>(element);
newnode->prev = before;
newnode->next = after;
after->prev = newnode;
if (before == NULL) {
first = newnode;
}
else{
before->next = newnode;
}
++size;
}

template <typename T>
typename List<T>::iterator List<T>::remove(iterator iter){
if(iter.position != NULL){
Node<T>* remove = iter.position;
Node<T>* before = remove->prev;
Node<T>* after = remove->next;
if (remove == first){
first = after;
} else{
before->next = after;
}
if (remove == last){
last = before;
}else{
after->prev = before;
}
iter.position = after;
--size;
delete remove;
return iter;
}else{
throw ElementNotFound();
}
}

template <typename T>
typename List<T>::iterator List<T>::begin(){
//iterator iter;
//iter.position = first;
//iter.last = last;
return iterator(first);
}

template <typename T>
typename List<T>::iterator List<T>::end(){
return iterator (last);
}


template <typename Predicate, typename T>
List<T>::iterator List<T>::find(const Predicate& predicate){
iterator iter;
for( iter = begin(); iter != end(); iter = next()){
if( predicate(iter.getElement())){
return iter;
}
}
return end();
}

template <typename Compare, typename T>
void List<T>::sort(const Compare& comparer){
Iterator<T> iter;
for( iter = begin(); iter != end(); iter = iter.next() ){
if( comparer( iter.getElement() , (iter+1).getElement()) != true){

}
}
}

template <typename T>
int List<T>::getSize() const{
return size;
}

template <class T>
List <T>::~List() {
Node <T> * firstNode = first;
while (firstNode != 0) {
Node <T> * nextNode = firstNode->next;
delete firstNode;
firstNode = nextNode;
}
}
template <typename T> class Node {
private:
T data;
Node* next;
Node* prev;
friend class List<T>;
friend class Iterator<T>;
public:
Node(T element){
data = element;
prev = NULL;
next = NULL;
}
~Node(){}
};

template <typename T> class Iterator{
private:
Node<T>* position;
Node<T>* last;
friend class List<T>;
public:
Iterator(){
position = NULL;
last = NULL;
}
Iterator(Node<T> * source): position(source) { }
T& getElement()const;
bool operator==(Iterator b) const;
bool operator!=(Iterator b) const;
T & operator*();
Iterator & operator++();
Iterator & operator++(int);
~Iterator(){}
};

template <class T>
T& Iterator<T>::getElement() const{
if(position != NULL){
return position->data;
}
else{
throw ElementNotFound();
}
}

template <typename T>
bool Iterator<T>::operator==(Iterator b) const{
return position == b.position;
}

template <typename T>
bool Iterator<T>::operator!=(Iterator b) const{
return position != b.position;
}

template <typename T>
T & Iterator<T>::operator*() {
return position->data;
}

template <class T>
Iterator<T> & Iterator<T>::operator++() {
position = position->next;
return *this;
}

template <class T>
Iterator<T> & Iterator<T>::operator++(int){
position = position->next;
return *this;
}


#endif /* LISTGENERIC_H_ */

最佳答案

要么您将sort 声明为一个模板函数,并在类中使用一个模板参数(Compare)。那么您的定义必须如下所示:

template <typename T>
template <typename Compare>
void List<T>::sort(const Compare& comparer) {

}

而且您还需要从 List 类中删除现在多余的 friend class Compare 声明。

或者您保持Comparesort 不变。在这种情况下,只需不要将 sort 作为模板,并省略 Compare 模板参数:

template <typename T>
void List<T>::sort(const Compare& comparer) {

}

当然,这只有在您在此函数之前定义(不仅仅是声明!)类Compare 时才有效。

但是第二种解决方案非常不正统而且毫无用处,因为 compare 参数没有多大意义:只有 一个 Compare 类,用户无法更改它。通常,这应该是一个模板参数(第一个解决方案)。

关于List的c++排序函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6444986/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com