C++:访问头实现文件中的类属性

C++: Accessing class properties in header implementation file

本文关键字:属性 文件 实现 访问 C++      更新时间:2023-10-16

我正在尝试实现双链接列表数据结构,因此我创建了一个具有类型为Node的私有属性node的类,当我尝试使用this关键字从函数实现访问此属性时,应用程序失败。我需要帮助

LinkedList.hpp 的头文件

#include <stdio.h>
template<class T>
class LinkedList{
    private :
    struct Node{
        T value;
        Node* next;
        Node* prev;
    }node;
    public :
        LinkedList();
        LinkedList(T item);
        void add(T item);
//        void get();
//        void insert();
//        void remove();
};

下面是头文件的实现。

#include "LinkedList.hpp"
template<class T>
LinkedList<T>::LinkedList(){
}

template<class T>
LinkedList<T>::LinkedList(T item){
}
template <class T>
void LinkedList<T>::add(T item){
    Node* node = new Node;
    node->value = item;
    node->prev = NULL;
    //Where the error is being generated
    node->next = this.node;
};

返回的错误显示:

/Users/mac/Documents/LinkedList/LinkedList/LinkedList.cpp:27:22: Member reference base type 'LinkedList<T> *' is not a structure or union

this是一个指针,正如错误消息所指出的那样。

用途:

this->node