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'm trying to build a function that receives two arguments. The first argument is a linked list, and the second is a void function that changes the data in all nodes.
but i get this error when i try to compile: invalid initialization of reference of type ‘int&’ from expression of type ‘const int’
from this line return this->m_node->getData();
in T& Queue<T>::Iterator::operator*()
Node
class:
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() const;
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>
T& Node<T>::getData() {
return this->m_data;
}
template<class T>
const T& Node<T>::getData() const {
return this->m_data;
}
Queue
class:
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);
T& front();
const T& front() const;
void popFront();
int size() const;
class Iterator;
Iterator begin() const;
Iterator end() const;
class EmptyQueue {};
private:
Node<T>* m_head;
Node<T>* m_tail;
int m_size;
};
template<class T>
Queue<T>::Queue() {
this->m_head=nullptr;
this->m_tail=nullptr;
this->m_size=0;
}
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);
}
}
template<class T>
void transform(Queue<T>& queue, void (*transformer)(T&)) {
if(queue.size()==Queue<T>::SIZE_EMPTY) {
return;
//throw Queue<T>::EmptyQueue();
}
for(Queue<int>::Iterator i = queue.begin();i != queue.end();++i) {
transformer(*i);
}
}
Queue<T>::Iterator
class:
template<class T>
class Queue<T>::Iterator {
public:
T& operator*();
Iterator& operator++();
Iterator operator++(int);
bool operator==(const Iterator& iterator) const;
bool operator!=(const Iterator& iterator) const;
Iterator(const Iterator&)=default;
Iterator& operator=(const Iterator&)=default;
class InvalidOperation {};
private:
const Node<T>* m_node;
Iterator(const Node<T>* m_node);
friend class Queue<T>;
};
template<class T>
Queue<T>::Iterator::Iterator(const Node<T>* m_node) {
this->m_node=m_node;
}
template<class T>
T& Queue<T>::Iterator::operator*() {
return this->m_node->getData();
}
Edit: main
:
static void setFortyTwo(int& n)
{
n = 42;
}
int main() {
Queue<int> queue5;
for (int i = 1; i <= 5; i++) {
queue5.pushBack(i);
}
transform(queue5, setFortyTwo);
return 0;
}
Aucun commentaire:
Enregistrer un commentaire