Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

a value of type "int" cannot be assigned to an entity of type "Node<int> *"C/C++(513)

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

>Solution :

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;
    //...
};
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading