c++ - 错误 C2440 : '=' : cannot convert from 'ListNode<T> *' to 'ListNode<T> *'

标签 c++ templates compiler-errors

我正在尝试创建一个双向链表,但收到此错误:

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

我真的不知道为什么。有人可以帮助我吗?

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;

...

这是导致错误的函数

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

这条线有问题。我正在尝试创建一个新节点并影响当前节点,该节点是指向 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对于 List 阴影 T对于 operator += 。您可以使用typename U对于 operator +=并尝试一下。
另外,您确定需要 operator += 的新类型吗? ?或者您打算使用与 List<T> 相同的类型.

附注,operator += 的典型语法是:

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

关于c++ - 错误 C2440 : '=' : cannot convert from 'ListNode<T> *' to 'ListNode<T> *' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9678120/

相关文章:

c++ - 嵌套在模板类中的模板类的前向声明

c++ - Makefile: "No rule to make target..."有多个工作目录

c++ - 使用 isalpha() C++ 的问题

c++ - 使用带有 std::shared_ptr 的 Qt 对象

c++ - 为什么二进制等效计算不正确?

c++ - 将派生对象分配给函数内的基类指针

css - Django 模板, 'include' css 而不是使用链接

c++ - 有什么工具可以帮助我阅读 c++ 模板编译错误吗?

python - 安装Python库时出错?

objective-c - Xcode 在没有时抛出随机错误