hii,
this is my .h ,file :

/*
 * 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 error when i try to compile it , with g++ :

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&)

any ideas ? is my SortedListIterator , written correctly ?

thnx

Recommended Answers

All 2 Replies

Please also provide sorted_list_example.cpp. However, both files should not need to be longer than about 15 lines to demonstrate the problem :)

#include "sorted_list.h"

#include <assert.h>

#include <iostream>



using mtm::SortedList;



class AbsoluteComparer{

public:

	bool operator()(int a, int b) const{

		a = a < 0 ? -a : a;

		b = b < 0 ? -b : b;

		return a<b;

	}

};



void SortedListExample(){

	std::less<int> compare;

	mtm::SortedList<int> list(compare);

	list.add(5);

	SortedList<int> list2(list);

	list.add(6);

	list.add(7);

	assert(list.remove(3) == true);

	assert(!list.contains(3));

	assert(list.size() == 4);

	assert(list2.size() == 3);

	list2 = list;

	assert(list2.size() == 4);



	int i=0;

	int elements[4] = {9, 7, 6, 5};

	for(SortedList<int>::iterator iterator=list.begin();

			iterator != list.end();iterator++){
 
		assert(*iterator == elements[i]);

		i++;

	}
}

this is a part of it , i get the error in the for operator!= .

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.