Cxsualty Gamin
Cxsualty Gamin

Reputation: 69

Struct and functions with templates

I just started using templates and want to implement it in this fashion within my header file, but I am getting various errors and don't really understand why.

Header File:

#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

Errors:

Img

Upvotes: 1

Views: 62

Answers (1)

asmmo
asmmo

Reputation: 7100

I think you forgot to put class list or something like that

#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

But If the functions are not member functions, you are wrong, so write them, as follows

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);

Upvotes: 1

Related Questions