我的程序没有发现结构中的函数

My program not discovery the functions in struct

本文关键字:函数 结构 发现 程序 我的      更新时间:2023-10-16

我的程序没有发现在structNode中创建的函数returnTest或其他新函数。g++编译器返回此错误:

linkedList.cpp: In instantiation of 'void LinkedList<T>::insert(T) [with T = int]':
linkedList.cpp:37:22:   required from here
linkedList.cpp:31:13: error: 'struct LinkedList<int>::Node' has no member named
'returnTest' std::cout << auxHead->returnTest();

我的文件是:

主要.cpp

#include "linkedList.hpp"
#include <iostream>
#include <cstdlib>
template<class T> LinkedList<T>::LinkedList()
{
    node = NULL;
}
template<class T> LinkedList<T>::~LinkedList()
{
}
template<class T> bool LinkedList<T>::isEmpty(){
}
template<class T> int LinkedList<T>::size(){
    if (node == NULL)
        return 0;
}
template<class T> void LinkedList<T>::insert(T element){
    Node *auxHead = node;
    if (auxHead == NULL){
        Node* newNode = new Node();
        newNode->data = element;
        node = newNode;
    }else{
        std::cout << auxHead->returnTest();
    }
}
int main(){
    LinkedList<int> *newLinked = new LinkedList<int>();
    newLinked->insert(55);
    newLinked->insert(55);
    return 0;
}

链接列表.hpp

#ifndef __LINKEDLIST_H_
#define __LINKEDLIST_H_
#include <stdio.h> 
template <class T> class LinkedList {
    private:
        struct Node {
            T data = NULL;
            Node *next;
            T getData(){
                return data;
            }
            Node getNext(){
                return next;
            }
            int returnTest(){
                return -1;
            }
            T isNIL(){
                return (data == NULL);
            }
        };
        Node *node = NULL;
    public:
        LinkedList();
        ~LinkedList();
        bool isEmpty();
        int size();
        void insert (T&);
};
#endif


我的g++版本是4.8.1。请忽略方法size()

不能实现模板类/函数。只需将实现(位于.cpp中)放入头文件即可。