Mathieu Lambert
Mathieu Lambert

Reputation: 41

error C2440: '=' : cannot convert from 'ListNode<T> *' to 'ListNode<T> *'

I'm trying to create a doubly linked list and I'm getting this error:

error C2440: '=' : cannot convert from 'ListNode<T> *' to 'ListNode<T> *'

and i really don't know why. Can someone can help me?

template <class T>
class ListNode
{
private:
    //Données membres
    T m_Value;
    ListNode<T> *m_Next;
    ListNode<T> *m_Prev;
public:
    //Constructeur
    ListNode()
    {
        m_Value = NULL;
        m_Next = NULL;
        m_Prev = NULL;
    }

    ListNode<T>(T _Value)
    {
        m_Value = _Value;
        m_Next = NULL;
        m_Prev = NULL;
    }

    //Accesseurs
    T getValue() const
    { return m_Value; }

    void setValue(T _Value)
    { m_Value = _Value; }

    ListNode<T>* getNext() const
    { return m_Next; }

    void setNext(ListNode *_NextNode)
    { m_Next = _NextNode; }

    ListNode<T>* getPrev() const
    { return m_Prev; }

    void setPrev(ListNode *_PrevNode)
    { m_Prev = _PrevNode; }
};

template <class T>
class List
{
private:
    //Données membres
    int m_Compte;
    ListNode<T> *m_Current;
    ListNode<T> *m_Head;
    ListNode<T> *m_Last;

...

Here is the function who cause the error

template <class T>
void operator+=(T _newValue)
{
        Where the error is

This line is the faulty one. I'm trying to create a new node and to affect the current node which is a pointer to a ListNode

    m_Current = new ListNode<T>(_newValue);

    if (!m_Head)
    {
        m_Head = m_Current;
    }
    else
    {
        m_Last->setNext(m_Current);
        m_Current->setPrev(m_Last);
        m_Last = m_Current;
    }
    ++m_Compte;
    return;
}

Upvotes: 2

Views: 733

Answers (2)

Andrew Lazarus
Andrew Lazarus

Reputation: 19330

I can't tell, because the entire class definition to the closing brace isn't here, whether operator+= is inside or outside the class definition.

If it is inside and you are defining it as an inline, then get rid of the template right above operator+=. The entire class is covered by the template declaration at the very top.

If it is being defined outside the class, then you need List::operator+= .

Upvotes: 0

iammilind
iammilind

Reputation: 69988

template <class T>  // <------ 'T'
class List
{
  ...
  template <class T>  // <------- 'T' again ?
  void operator+=(T _newValue);
};

Seems that, T for List shadows T for operator +=. You may use typename U for operator += and give a try.
Also, are you sure that, you want a new type for operator += ? Or do you intend to use the same type as List<T>.

On side note, typical syntax for operator += is:

List& operator += (const List &copy)
{
  ...
  return *this;
}

Upvotes: 3

Related Questions