自己的迭代器的C ++问题

时间:2013-12-22 13:53:22

标签: c++ iterator iteration

Edit1:代码翻译成英文。

Edit2:iteratorconst_iterator定义已从DoubleList.cpp移至DoubleList.h(感谢凯西)。

我正在使用迭代器创建自己的容器(抽象双列表)。我在谷歌,cplusplussome Helsinky上寻找了很多关于迭代器的信息,但我仍然在StackOverFlow上搜索,但我仍然感到很困惑。我会给你部分代码,我将尝试在内容中解释它,我不明白。主要是我不明白为什么我在第13行StudentRecord.cpp出现错误:

  • 8智能感知:没有操作员" ++"匹配这些操作数
  • 7智能感知:不允许不完整类型
  • 错误6错误C2678:二进制'!=' :找不到哪个运营商需要 类型' int'的左手操作数(或者没有可接受的 转化率)
  • 错误4错误C2440:'初始化' :无法转换 ' SemestralWork :: DoubleList:迭代'到' int'
  • 错误2错误C2079:'它'使用未定义的类 ' SemestralWork :: DoubleList:迭代'
  • 错误3错误C2027:使用未定义类型 ' SemestralWork :: DoubleList:迭代'
  • 错误5错误C2027:使用未定义类型 ' SemestralWork :: DoubleList:迭代'

我会很高兴看到每一条建议,并为我糟糕的英语抱歉。

StudentRecord.cpp(只是部分)

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(只是迭代器,开始,结束)

#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};
};

0 个答案:

没有答案