Edit1: Code translated to english.
Edit2: iterator
and const_iterator
definitions moved from DoubleList.cpp
to DoubleList.h
(thanks to Casey).
I'm creating my own container (Abstract double list) with iterator. I was looked for many informations about iterators on google, cplusplus, some Helsinky and still I'm searching on StackOverFlow, but I'm still really confused. I will give you parts of my codes and I will try to explain it in coments, what I don't understand. Mainly I don't understand why I have errors in StudentRecord.cpp
on line 13:
- 8 IntelliSense: no operator "++" matches these operands
- 7 IntelliSense: incomplete type is not allowed
- Error 6 error C2678: binary '!=' : no operator found which takes a left-hand operand of type 'int' (or there is no acceptable conversion)
- Error 4 error C2440: 'initializing' : cannot convert from 'SemestralWork::DoubleList::iterator' to 'int'
- Error 2 error C2079: 'it' uses undefined class 'SemestralWork::DoubleList::iterator'
- Error 3 error C2027: use of undefined type 'SemestralWork::DoubleList::iterator'
- Error 5 error C2027: use of undefined type 'SemestralWork::DoubleList::iterator'
I will be glad for every advice and sorry for my bad english.
StudentRecord.cpp (just part)
1. #include "StdAfx.h"
2. #include "StudentRecord.h"
3. using namespace SemestralWork;
4.
5. StudentRecord::StudentRecord(void)
6. {
7. }
8.
9. Student &StudentRecord::SearchStudent(const string &ID){
10. Student * SearchedStudent;
11. Student * EmptyStudent = NULL;
12.
13. for(DoubleList<Student>::iterator it = List->begin(); it != List->end(); ++it){
14. if(ID == List->AccessActual().getID()){
15. SearchedStudent = &List->AccessActual();
16. return *SearchedStudent;
17. }
18. }
19. return *EmptyStudent;
20. }
StudentRecord.h
#pragma once
#include <string>
#include <iostream>
#include <algorithm>
#include "Student.h"
#include "DoubleList.h"
using namespace std;
using namespace SemestralWork;
class StudentRecord{
private:
DoubleList<Student> *List;
public:
StudentRecord(void);
~StudentRecord(void);
Student &SearchStudent(const string &ID);
void addStudent(const Student &student, Student::Position position = Student::Position::LAST);
Student RemoveStudent(const string &ID);
void WriteAllStudents(void);
void Cancel(void);
};
DoubleList.cpp (just iterators, begins, ends)
#include "StdAfx.h"
#include "DoubleList.h"
using namespace SemestralWork;
...
template<typename T>
typename DoubleList<T>::iterator DoubleList<T>::begin(){
return AccesFirst(); // = access first value
}
template<typename T>
typename DoubleList<T>::iterator DoubleList<T>::end(){
return AccessLast(); // = access last value
}
template<typename T>
typename DoubleList<T>::const_iterator DoubleList<T>::cbegin(){
return AccesFirst(); //or return const_iterator(this); ??
}
template<typename T>
typename DoubleList<T>::const_iterator DoubleList<T>::cend(){
return AccessLast(); //or return const_iterator(); ??
}
DoubleList.h
#pragma once
#include <iostream>
#include <stdlib.h>
namespace SemestralWork{
template <typename T>
class DoubleList {
protected:
template <class U>
class Node{
Node<U> *next;
Node<U> *previous;
U *data;
public:
Node(U *data){
next= NULL;
previous= NULL;
this->data = data;
}
void SetNext(Node<U> *next) {
this->next = next;
}
Node<U> *GetNext(){ return next; }
void SetPrevious(Node<U> *previous) {
this->previous= previous;
}
Node<U> *GetPrevious(){ return previous; }
U *GetData() { return data; }
};
private:
Node<T> * actual;
Node<T> * first;
Node<T> * last;
int numberOfElements;
public:
typedef void (*PointerFunction)(const T);
DoubleList(void);
~DoubleList(void);
bool IsEmpty(void) const;
int NumberOfElements() const;
void AddFirst(const T &);
void AddLast(const T &);
void AddNext(const T &);
void AddPrevious(const T &);
T &AccessActual(void);
T &AccesFirst(void);
T &AccessLast(void);
T &AccesNext(void);
T &AccesPrevious(void);
T RemoveFirst(void);
T RemoveLast(void);
T RemoveActual(void);
T RemoveNext(void);
T RemovePrevious(void);
void Search(PointerFunction) const;
void Cancel(void);
class iterator {
public:
typedef T value_type;
typedef ptrdiff_t difference_type; //maybe int
typedef T* pointer;
typedef T& reference;
typedef std::forward_iterator_tag iterator_category;
typedef iterator self_type;
Node<T> * ptr;
iterator(); //just like that?
iterator(const iterator& mit){} //just like that?
~iterator();
iterator& operator++(){
ptr = ptr->GetNext();
return *this;
}
iterator operator++(int){
iterator tmp = *this; //iterator tmp(*this)
operator++();
return tmp;
}
bool operator==(const iterator& rhs) {return ptr==rhs.ptr;}
bool operator!=(const iterator& rhs) {return ptr!=rhs.ptr;}
Node<T>& operator*() {return ptr->GetData();}
Node<T>* operator->(){return ptr->GetData();}
};
class const_iterator{
//not programmed yet...
};
iterator begin();
iterator end();
const_iterator cbegin();
const_iterator cend();
};
}
Student.h
#pragma once
#include <string>
using namespace std;
class Student
{
private:
string firstName, lastName, ID;
public:
Student(string, string, string);
~Student(void);
string getFirstName();
string getLastName();
string getID();
enum Position{ FIRST, LAST, ACTUAL, PREVIOUS, NEXT};
};