c++ - 结构和模板功能

标签 c++ templates struct

我刚刚开始使用模板,并想以这种方式在头文件中实现它,但是我遇到了各种各样的错误,并且并不清楚为什么。

头文件:

#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

错误:

Img

最佳答案

我认为您忘了输入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/

相关文章:

c++ - 将一行数字数据读入一个数组,然后将下一行读入一个变量。 C++

c++ - 最近点算法 |如何改进?

django模板: Is extended template able to change css in parent page?

c - 取消引用指向不完整类型的指针 ‘struct'

c - 使用结构时的 mmap 偏移量

c++ - 从 C++ 中的主函数返回字符串

c++ - 如何将文件中的团队信息解析并存储到类中?

c++ - 成员访问和模板特化

c++ - 我收到有关我的模板类的错误

C 无法向结构添加新元素