Frage

I have implemented my own doubly linked list, which has templated nodes. Through this node I have stored my own object (Play) which has get gunctions, and I have created an iterator for my list.

for(DLL<Play>::iterator itr=PlayList.begin(); itr!=PlayList.end(); itr++)
                if(offense==*how do I access this*getoTeam()){

Here is my node and iterator code:

template<class T>
class node{
    public:
    T data;
    node *next, *prev;
    node(const T& t, node<T> *n = 0, node<T> *p = 0) {data=t; next=n; prev=p;}
    node() {next = prev = 0;}
};

template<typename T>
class Iterator{
    public:
        Iterator(node<T> *np=0) : nodePtr(np) {}

        T& operator*() const {return nodePtr->data;}

        Iterator<T> &operator++(){
            nodePtr = nodePtr->next;
            return *this;
        }

        Iterator<T> operator++(int){
            Iterator<T> Temp(nodePtr);
            nodePtr = nodePtr->next;
            return Temp;
        }

        bool operator==(Iterator<T> i) { return nodePtr == i.nodePtr; }
        bool operator!=(Iterator<T> i) { return nodePtr != i.nodePtr; }

    private:
        node<T> * nodePtr;
};

I'm at a loss for what I would put inside how do I access this. If you could explain why as well, I'd appreciate it.

Thanks.

And just incase, here is my Play object:

#ifndef PLAY_H_INCLUDED
#define PLAY_H_INCLUDED
#include <string>

class Play
{
private:
    int m_quarter;
    int m_minutes;
    std::string oTeam;
    std::string dTeam;
    int m_down;
    int m_yardToGO;
    int m_startLoc;
    int playDesc;
    std::string wholePlay;
public:
    int getQuarter();
    int getMinutes();
    std::string getoTeam();
    std::string getdTeam();
    int getDown();
    int getYard();
    int getStartLoc();
    int getPlayDesc();
    std::string getwholePlay();
    Play(int quarter, int minutes, std::string offense, std::string defense, int dwn, int ytg, int start, int desc, std::string play);
    ~Play();
    Play parse(std::string toParse);
    std::string findPlay(std::string playDesc);
};

#endif // PLAY_H_INCLUDED
War es hilfreich?

Lösung

Assuming itr is of type Iterator<Play>, use offense == (*itr).getoTeam().

*itr calls Iterator<Play>::operator*() returning a Play& on which you can then call getoTeam(). For consistency, you should also implement

T* Iterator::operator->() {return &nodePtr->data;}

This way, you could use the more familiar notation offense == itr->getoTeam()

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top