Home > Blockchain >  a value of type "int" cannot be assigned to an entity of type "Node<int> *"
a value of type "int" cannot be assigned to an entity of type "Node<int> *"

Time:05-25

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

CodePudding user response:

Queue<T>::front() is returning a Node<T>*& when it should return a T&.

Example:

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

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

You also need to make the same change in the class definition:

template <class T>
class Queue {
public:
    //...
    T& front();
    const T& front() const;
    //...
};

CodePudding user response:

The problem is that Queue<T>::front returns a Node<T>* by reference and so by writing queue1.front() = 3; you're trying to assign an int to an Node<int>*.

queue1.front() = 3;

In the above statement, the left hand side result has a type Node<int>* but the right hand side is 3 which is an int. Thus the type on the left hand side and right hand side mismatch and since there is no implicit conversion from an int to a Node<int>*, you get the mentioned error:

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