Convert 'const Std::string' to 'Std::string &'

Asked

Viewed 77 times

0

I have a mistake and would like tips to fix it

I have my Node struct inside the Dllist class:

struct Node
{
    Type m_data;
    Node *m_next, *m_prev;

    Node(Type& v, Node* _prev) : m_data(v), m_prev(_prev), m_next(nullptr) {}
};

Node *m_head, *m_tail;
int m_size;

And the function:

template<typename Type>
void DLList<Type>::addHead(const Type& v)
{
    Node* node = new Node(v, m_head);

    node->m_data = v;
    node->m_next = m_head;
    m_head = node;

    ++m_size;
}

But when creating Node* Node by passing the parameters, the compiler is returning this error:
'Dllist::Node::Node(Dllist::Node &&)': cannot Convert argument 1 from 'const Std::string' to 'Std::string &'

The parameters passed are coming from main():

DLList<string> list;
DLList<string> list2;
DLList<string>* list3;
DLLIter<string> iter(list);
DLList<int>* testPointer;

1 answer

0

I just found the mistake... really lack of attention.

My Node constructor is prepared to receive only 'Type& v', while I am trying to pass 'const Type& v', which logically would not work.

New struct Node:

struct Node
{
    Type m_data;
    Node *m_next, *m_prev;

    Node(const Type& v, Node* _prev) : m_data(v), m_prev(_prev), m_next(nullptr)    {}
};

Browser other questions tagged

You are not signed in. Login or sign up in order to post.