I'm trying to understand how including works in C++
. I have two questions about it. The first one is on how properly import the .h
file. For example I created the following HashNode.h
file:
namespace HashNode{
template<class Data>
class HashNode{
private:
Data data;
HashNode *next;
public:
explicit HashNode(const Data &data);
Data getKey();
~Node();
};
}
So in the HashNode.cpp
file, it should like:
#include "HashNode.h"
using namespace HashNode;
template <class Data> // ~~~ HERE 1 ~~~
HashNode::HashNode(const Data &data) {//todo};
template <class Data> // ~~~ HERE 2 ~~~
Data* HashNode::getKey() {
//todo
}
HashNode::~Node() {
//todo
}
This way it works but do I have to include template <class Data>
beside each function which uses Data
? Why it does not recognize Data
without including template <class Data>
?
Also I have created the Hash.h
file which should use the HashNode.h
file:
#include "HashNode.h"
using namespace HashNode;
namespace Hash {
template <class Data>
class Hash {
typedef enum {
GOOD = 0,
BAD = -1,
BAD_ALLOC = -2
} Status;
private:
HashNode **hash;
int capacity;
int size;
public:
explicit Hash(int size);
Status insertData(const Data &data);
~Hash();
};
}
But I get the the following error: Can't resolve type 'HashNode'
. Why it can't see the import?
In the Hash.cpp
file I get Unused import statement
for #include "HashNode.h"
. Why is that?
Also, what if I want to include private functions - should them be in the .h
file or in the .cpp
file?