我的程序没有发现函数returnTest或其他在struct Node中创建的新函数。 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();
我的档案是:
#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;
}
#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。请忽略方法大小()
答案 0 :(得分:0)
您无法执行模板类/功能。只需将实现(在.cpp中)放入头文件中。