default parameters in template
Jan 17, 2011 at 3:53pm UTC
hii every one i want to use std::less (function object) as a default in a class template like so :
template <class T,class S>
if you don't know , less is in <functional> header and it's also a template see :
http://www.cplusplus.com/reference/std/functional/less/
i tried :
1 2 3 4
template <class T,class S=std::less>
class SortedList{
SortedList(S compare=less);
}
i want to be able to define sortedList as :
1 2 3 4 5 6 7 8 9 10 11
1)
SortedList<int ,any compare function> list(compare);
2)
CompareFunction compare;
SortedList<int > list(compare);
3)
std::less<int > compare;
mtm::SortedList<int > list(compare);
Jan 17, 2011 at 4:35pm UTC
This is what the STL does:
1 2 3 4 5 6 7 8 9 10
template < typename T, typename Compare = std::less<T> >
class SortedList
{
private :
Compare comp;
public :
SortedList() : comp() {}
explicit SortedList( const Compare& c ) : comp( c ) {}
};
The constructor that takes a compare function can take a compare function only of the
given template parameter type. The idea is that if your compare function needs to be
constructed with a parameter (instead of default constructed) you would use the second
form above. Otherwise the first form, which default constructs the comparator, works.
Jan 17, 2011 at 7:48pm UTC
thnx it worked ,
but know i get this problem that i don't know why ?
i get :
1 2 3 4 5
sorted_list_example.cpp:49: error: no match for ‘operator !=’ in ‘iterator != mtm::SortedList<T, Compare>::end() [with T = int , Compare = std::less<int >]()’
sorted_list.h:95: note: candidates are: bool mtm::operator !=(mtm::SortedList<int , std::less<int > >::SortedListIterator&, mtm::SortedList<int , std::less<int > >::SortedListIterator&)
sorted_list_example.cpp:75: error: no match for ‘operator !=’ in ‘iterator != mtm::SortedList<T, Compare>::end() [with T = int , Compare = AbsoluteComparer]()’
sorted_list.h:95: note: candidates are: bool mtm::operator !=(mtm::SortedList<int , AbsoluteComparer>::SortedListIterator&, mtm::SortedList<int , AbsoluteComparer>::SortedListIterator&)
sorted_list.h:95: note: bool mtm::operator !=(mtm::SortedList<int , std::less<int > >::SortedListIterator&, mtm::SortedList<int , std::less<int > >::SortedListIterator&)
and my .h , is :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
/*
* sortedlist.h
*
* Created on: Jan 16, 2011
* Author: saeed hardan
*/
#ifndef SORTEDLIST_H_
#define SORTEDLIST_H_
#include <iostream>
#include <functional>
namespace mtm {
template < typename T, typename Compare = std::less<T> >
class SortedList {
private :
struct ListNode {
ListNode *next;
ListNode *prev;
T data;
ListNode() {
next = prev = NULL;
}
}*head,*tail;
int length;
Compare comp;
void addFirst(T element){}
void removeFirst(T element){}
void removeLast(T element){}
public :
/******************************************************************************
*
*****************************************************************************/
class SortedListIterator {
private :
ListNode *cur;
public :
SortedListIterator() :
cur(NULL) {
}
SortedListIterator(ListNode *pos) :
cur(pos) {
}
SortedListIterator(const SortedListIterator& iter){
cur = iter.cur;
}
T& operator *(){
return cur->data;
}
SortedListIterator& operator ++(){ // Prefix
cur=cur->next;
return (*this );
}
SortedListIterator operator ++(int ){ // Postfix
SortedListIterator curPosition((*this ));
++(*this );
return curPosition;
}
friend bool operator ==
(SortedListIterator& iterator1, SortedListIterator& iterator2){
if (iterator1.cur == iterator2.cur){
return 1;
}
return -1;
}
friend bool operator !=
(SortedListIterator& iterator1, SortedListIterator& iterator2){
return !(iterator1==iterator2);
}
};
/******************************************************************************
*
*****************************************************************************/
typedef SortedListIterator iterator;
iterator begin() {
iterator iter(head);
return iter;
}
iterator end() {
iterator iter(NULL);
return iter;
}
/******************************************************************************
*
*****************************************************************************/
SortedList():head(new ListNode),tail(head),length(0),comp(){}
explicit SortedList(const Compare& c):
head(new ListNode),tail(head),length(0),comp(c){}
SortedList(const SortedList& sortedList){}
~SortedList(){}
SortedList& operator =(const SortedList& sortedList){ }
/******************************************************************************
*
*****************************************************************************/
int size() const {}
void add(T element){ }
bool contains(T element){}
bool remove(T element){}
};
}
#endif /* SORTEDLIST_H_ */
i get this although i have defined operator != , operator == , inside SortedListIterator as a public , why ?
thnx in advance .
Last edited on Jan 17, 2011 at 7:49pm UTC
Jan 18, 2011 at 12:29pm UTC
is my SortedListIterator , implemented correctly .??
Jan 18, 2011 at 1:45pm UTC
The parameters to both operator== and operator!= need to be const references.
Jan 18, 2011 at 2:41pm UTC
thnx a lot , you know c++ , i mean know know ;)
Topic archived. No new replies allowed.