mercredi 25 mai 2022

a value of type "int" cannot be assigned to an entity of type "Node

I have a linked list that contains a pointer to the first and last node and size which indicates how many nodes are there in the list. I have a function that returns the first node.

I want to be able to change the m_data in the first node using queue1.front() = 3;. However, I am getting

invalid conversion from ‘int’ to ‘Node<int>*’

error while compiling

template <class T>
class Node {
public:
    Node(const T& t);
    ~Node() = default;            // Destructor
    Node(const Node&) = default;  // Copy Constructor set to default
    Node& operator=(const Node&) =
        default;  // Assignment operator set to default
    T& getData();
    const T& getData() const;
    Node* getNext();
    void setNext(Node<T>* newNext);

private:
    T m_data;
    Node* m_nextNode;
};

template <class T>
Node<T>::Node(const T& t) {
    this->m_data = t;
    this->m_nextNode = nullptr;
}
template <class T>
class Queue {
public:
    static const int SIZE_EMPTY = 0;
    Queue();
    ~Queue();                       // Destructor
    Queue(const Queue&) = default;  // Copy Constructor set to default
    Queue& operator=(const Queue&) =
        default;  // Assignment operator set to default
    void pushBack(const T& t);
    Node<T>*& front();
    const Node<T>*& front() const;
    void popFront();
    int size() const;
    class EmptyQueue {};

private:
    Node<T>* m_head;
    Node<T>* m_tail;
    int m_size;
};

template <class T>
Node<T>*& Queue<T>::front() {
    if (this->m_size == Queue<T>::SIZE_EMPTY) {
        throw Queue<T>::EmptyQueue();
    }
    return this->m_head;
}

template <class T>
void Queue<T>::pushBack(const T& t) {
    this->m_size += 1;
    Node<T>* newNode = new Node<T>(t);
    this->m_tail = newNode;
    if (this->m_size == 1) {
        this->m_head = newNode;
    } else {
        Node<T>* tempNode = this->m_head;
        while (tempNode->getNext()) {
            tempNode = tempNode->getNext();
        }
        tempNode->setNext(newNode);
    }
}
int main() {
    Queue<int> queue1;
    queue1.pushBack(1);
    queue1.front() = 3;
}

Aucun commentaire:

Enregistrer un commentaire