簡體   English   中英

模板類指針問題 C++

[英]Template class pointers issue c++

我想知道為什么在使用模板類時會出現這些錯誤。 我收到錯誤 C2440 '=' 無法從 ' node<T> * ' 轉換為 ' node<T> * '。 這看起來很奇怪,因為它們是完全相同的類型。

    template<class T>
    struct node
    {        
        T value;
        node<T> *next = nullptr;
        node()
        {
            this->next = nullptr;
        }

        node(T t)
        {
            this->value = t;
            this->next = nullptr;
        }

        node(T t, node<T> *nextpointer)
        {
            this->value = t;
            this->next = nextpointer;
        }

        ~node()
        {

        }
    };

    template<class T> class forwardList
    {
    private:
        node<T> *head;
    public:
        template<class T> forwardList() {}
        template<class T> forwardList(T var)
        {
            if (head == nullptr)
            {
                node<T> *firstNode = new node<T>(var);
                this->head->next = firstNode; //Here it doesn't work for me
            }
        }
    };

在進行以下更改后,您的代碼對我來說編譯得很好(在 MSVC 上):

  • 刪除頂部附近的enter code here
  • 刪除forwardList構造函數聲明前面的template<class T>

     public: forwardList() {} forwardList(T var) { ...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM