为什么我会收到错误:无效使用会员(你忘了'&')?

时间:2016-10-14 23:17:33

标签: c++ queue nodes

当我尝试编译popRight或popLeft时出现此错误。为什么?这是代码。

出发课程

  #include "node.h"
template <class T>
class Dequeue
{
    public:
        void pushLeft(T data);
        void popLeft();
        T left();
        void pushRight(T data);
        void popRight();
        T right();
        bool empty();
    private:
        int length;
};

template<typename T>
bool Dequeue<T>::empty()
{
    if (length == 0)
    {
        return true;
    }
    else
        return false;
}

template<typename T>
void Dequeue<T>::pushLeft(T data)
   {
        if (empty() == true)
        {
            left->setData(data);
            right->setData(data);
        }
        else if (left->getData() == right->getData())
        {
            left->setData(data);
        }
        else
        {
            node<T> *aux = new node<T>;
            aux->setData(data);
            left->setPrevious(aux);
            aux->setNext(left);
            left = aux;
        }
        length++;

    }

template<typename T>
  void Dequeue<T>::pushRight(T data)
   {
        if (empty() == true)
        {
            right->setData(data);
            left->setData(data);
        }
        else if (right->getData() == left->getData())
        {
            right->setData(data);
        }
        else
        {
            node<T> *aux = new node<T>;
            aux->setData(data);
            right->setPrevious(aux);
            aux->setNext(right);
            right = aux;
        }
        length++;
    }

template<typename T>
 void Dequeue<T>::popLeft()
    {
        node<T> *node = left->getNext();
        node->setPrevious(NULL);
        left = node;
    }

template<typename T>
 void Dequeue<T>::popRight()
 {
        node<T> *node = right->getPrevious();
        node->setNext(NULL);
        right = node;
  }

节点类

 template <class T>
class node
{
    public:
        node(T data);
        node(T data, node<T>* next);
        T getData();
        node<T>* getNext();
        void setData(T data);
        void setNext(node<T>* next);
        void setPrevious(node<T>* previous);
    private:
        T data;
        node<T> *next;
        node<T> *previous;
        node<T> *left;
        node<T> *right;
};

template <typename T>
node<T>::node(T data)
{
    this->data = data;
    this->next = NULL;
    this->previous = NULL;
}

template <typename T>
node<T>::node(T data, node<T>* next)
{
    this->data = data;
    this->next = next;
    this->previous = previous;
}

template <typename T>
T node<T>::getData()
{
    return this->data;
}

template <typename T>
node<T>* node<T>::getNext()
{
    return this->next;
}

template <typename T>
void node<T>::setData(T data)
{
    this->data=data;
}

template <typename T>
void node<T>::setNext(node<T>* next)
{
    this->next = next;
}

template <typename T>
void node<T>::setPrevious(node<T>* previous)
{
    this->previous = previous;
}

我一直试图解决这个问题一个小时,但我不能,任何人都可以帮忙吗?

1 个答案:

答案 0 :(得分:3)

您宣布leftright为函数。但是从你在其余代码中使用它们的方式可以清楚地知道它们应该是变量。

class Dequeue
{
    public:
        void pushLeft(T data);
        void popLeft();
        T left;
        void pushRight(T data);
        void popRight();
        T right;
        bool empty();
    private:
        int length;
};