错误C2440:'=':无法从'ListNode< T> *'到'ListNode< T> *' [英] error C2440: '=' : cannot convert from 'ListNode<T> *' to 'ListNode<T> *'

查看:475
本文介绍了错误C2440:'=':无法从'ListNode< T> *'到'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

这行是错误的。我试图创建一个新节点并影响当前节点,它是一个指向ListNode的指针。

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


推荐答案

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

似乎 T 代表 列表 阴影 T 。您可以使用 typename U 替换运算符+ = 并尝试。

你确定,你想要一个新类型 operator + = ?或者您打算使用与列表< T> 相同的类型。

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>.

operator + = 是:

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

这篇关于错误C2440:'=':无法从'ListNode&lt; T&gt; *'到'ListNode&lt; T&gt; *'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆