I have to build a doubly linked list, but I was presented with an error that I can not solve:
Node class
It is important to note that this class makes use of template
to accept any format.
#ifndef nodo_h
#define nodo_h
#include <iostream>
using namespace std;
template<class type>
class nodo
{
private:
type element;
nodo *next, *previous;
public:
void SetElement(type element);
void ShowElement();
type GetElement();
nodo<type>* GetNext();
nodo<type>* GetPrevious();
nodo<type>();
~nodo<type>();
};
Class list8
The problems are presented in the procedures SetFirst
and SetLast
I forgot to say that this list must also use template
to be able to send variables from main
, so first access to lista8
where there is a list not yet defined of the class nodo
#ifndef lista8_h
#define lista8_h
#include "nodo.h"
template<class type>
class lista8
{
private:
nodo<type> *first, *last;
public:
void SetFirst(type NewElement); //<-error
void SetLast(type NewElement); //<-error
int ClearFirst();
int ClearLast();
void ShowList(nodo<type> *list);
nodo<type>* GetFirst();
nodo<type>* GetLast();
lista8<type>();
~lista8<type>();
};
Here is the code of SetFirst
and SetLast
, including where the error is,
the error that visual studio shows me is what it says in the title .
(being more specific, it says: C2106 '=': the operand must be value L)
Where it says 'error that I expected' is where I imagined that this error would also appear, but interestingly, it does not tell me that it is incorrect .
template<class type>
void lista8<type>::SetFirst(type NewElement)
{
if (first == NULL)
{
first = new nodo<type>; //<--error que esperaba
last = first;
}
else
{
first->GetPrevious() = new nodo<type>; //<--error
first->GetPrevious()->GetNext() = first; //<--error
first = first->GetPrevious();
}
first->SetElement(NewElement);
}
template<class type>
void lista8<type>::SetLast(type NewElement)
{
if (last == NULL)
{
last = new nodo<type>; //<--error que esperaba
first = last;
}
else
{
last->GetNext() = new nodo<type>; //<--error
last->GetNext()->GetPrevious() = last; //<--error
last = last->GetNext();
}
last->SetElement(NewElement);
}