This is my code after lots of changes that I did.I have a problem with to functions (sort and find) but I still can't understand how to solve it.
using namespace std;
using namespace ListExceptions;
template <typename T> class Node;
template <typename T> class Iterator;
/*List class*/
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(const T element));//line 47
void sort(const Compare& comparer());//line 48
int getSize() const;
iterator begin();
iterator end();
~List();
};
/*Implimentation of List functions*/
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(){
return iterator(first);
}
template <typename T>
typename List<T>::iterator List<T>::end(){
if(size != 0){
return iterator(last);
}
throw ElementNotFound();
}
//TODO
template <typename T>
template <typename Predicate>
typename List<T>::iterator List<T>::find(const Predicate& predicate(const T element)) {//line145
iterator iter;
Node<T>* current = iter.position;
Node<T>* end = iter.last;
for( ; current != end; ++current){
if( predicate(*current)){
return current;
}
}
return end;
}
//TODO
template <typename Compare>
template <typename T>
void List<T>::sort(const Compare& comparer()){//line154
Iterator<T> current = position;
Iterator<T> end = last;
for( ; current != end; ++current){
if( comparer( *current , *current+1) != 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;
}
}
/*Node Class*/
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(){}
};
/*Iterator class*/
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(){}
};
/*Implimentation of Iterator class function*/
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;
}
The messages that I recieve are:
C:/Programs/eclipse-with-gcc/eclipse/mingw/bin/../lib/gcc/mingw32/3.4.5/../../../../include/c++/3.4.5/cwchar:161: error: `::swprintf' has not been declared
C:/Programs/eclipse-with-gcc/eclipse/mingw/bin/../lib/gcc/mingw32/3.4.5/../../../../include/c++/3.4.5/cwchar:168: error: `::vswprintf' has not been declared
..\list.h:140: error: expected constructor, destructor, or type conversion before "List"
..\list.h:154: error: no member function `sort' declared in `List<T>'
..\list.h:154: error: invalid function declaration
Looks to me like you dependent names (List<T>::iterator
, in particular), and thus need to use typename
.
template <typename T>
template <typename Predicate>
List<T>::iterator List<T>::find(const Predicate& predicate(const T element)) {
template <typename Compare>
template <typename T>
void List<T>::sort(const Compare& comparer()) {
It looks like you are declaring some kind of function when function argument is expected. Try
List<T>::find(const Predicate& predicate)
List<T>::sort(const Compare& comparer)
精彩评论