1

I have a template class called item. I have another class having array of items - class hashTable. I have third class which derived from hashTable - HSubject. I'm confused about the using/include that I have to add to each class. I tried many options but it produced run time errors.

code is attached here:

*item.h*

#pragma once
#include "stdafx.h"
#include <iostream>
#include <list>
#include <string>
#include <vector>
using namespace std;

enum state { empty, full, deleted };

template <class T, class K>
class item
{
// propeties and functions here
};
*hashTable.h*

#pragma once
#include <iostream>
#include <list>
#include <string>
#include <vector>
#include "item.h"

using namespace std;

// tried adding this but didn't seem to work
//template <class T, class K>
//class item<T, K>;

template <class T, class K>
class hashTable
{
public: 
    vector<item<T, K>> hashT;
    hashTable();
    // propeties and functions here
};
*hashTable.cpp*

#pragma once
#include "stdafx.h"
#include <iostream>
#include <list>
#include <string>
#include <vector>
#include <memory>
#include "hashTable.h"
using namespace std;

template<class T, class K>
hashTable<T, K>::hashTable() 
{}
// functions are written here
*HSubject.h*

#pragma once
#include "stdafx.h"
#include <iostream>
#include <list>
#include <string>
#include <vector>
#include <memory>

class item<list<string>, string>;

using namespace std;
class HSubject :
    public hashTable<list<string>, string>
{
public:
    HSubject();
    // propeties and functions here
};
*HSubject.cpp*

#pragma once
#include "stdafx.h"
#include <iostream>
#include <list>
#include <string>
#include <vector>
#include <memory>
#include "hashTable.h"
#include "HSubject.h"
#include "item.h"
using namespace std;

HSubject::HSubject() : hashTable()
{
}
// functions are written here

Any help would be greatful. Thanks!

moriahs
  • 25
  • 4
  • 1
    You almost certainly want to implement all the member functions of a class template in its header file. – Davis Herring Nov 25 '20 at 21:30
  • 1
    In particular, see [Why can templates only be implemented in the header file?](https://stackoverflow.com/questions/495021/why-can-templates-only-be-implemented-in-the-header-file) – Igor Tandetnik Nov 25 '20 at 22:48

0 Answers0