我刚刚开始使用模板,并想以这种方式在头文件中实现它,但是我遇到了各种各样的错误,并且并不清楚为什么。
头文件:
#ifndef EXAM3_H_
#define EXAM3_H_
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
using namespace std;
const int maxInt = 2147483647;
template <typename T>
struct nodeType {
T data;
nodeType *next;
};
bool search(nodeType *head, const T &searchItem);
void deleteNode(nodeType *&head, nodeType *&tail, int &count, const T &deleteItem);
void initializeList(nodeType *&head, nodeType *&tail, int &count);
bool isEmptyList(const nodeType *head);
void printList(nodeType *head);
int lengthList(nodeType *head);
void destroyList(nodeType *&head, nodeType *&tail, int &count);
void insert(const T &newItem, nodeType *&head, nodeType *&tail, int &count);
#endif
错误:
最佳答案
我认为您忘了输入class list
或类似的内容
#ifndef EXAM3_H_
#define EXAM3_H_
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
using namespace std;
const int maxInt = 2147483647;
template <typename T>
class list{
public:
struct nodeType {
T data;
nodeType *next;
};
bool search(nodeType *head, const T &searchItem);
void deleteNode(nodeType *&head, nodeType *&tail, int &count, const T &deleteItem);
void initializeList(nodeType *&head, nodeType *&tail, int &count);
bool isEmptyList(const nodeType *head);
void printList(nodeType *head);
int lengthList(nodeType *head);
void destroyList(nodeType *&head, nodeType *&tail, int &count);
void insert(const T &newItem, nodeType *&head, nodeType *&tail, int &count);
};
#endif
但是,如果这些函数不是成员函数,那么您错了,因此请按如下所示编写它们
template<typename T>
bool search(nodeType <T> *head, const T &searchItem);
template<typename T>
void deleteNode(nodeType <T> *&head, nodeType <T> *&tail, int &count, const T &deleteItem);
template<typename T>
void initializeList(nodeType <T> *&head, nodeType <T> *&tail, int &count);
template<typename T>
bool isEmptyList(const nodeType <T> *head);
template<typename T>
void printList(nodeType <T> *head);
template<typename T>
int lengthList(nodeType <T> *head);
template<typename T>
void destroyList(nodeType <T> *&head, nodeType <T> *&tail, int &count);
template<typename T>
void insert(const T &newItem, nodeType <T> *&head, nodeType <T> *&tail, int &count);
关于c++ - 结构和模板功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61786418/