I am just confused as to what my file is supposed to look like. I am not sure on the syntax as well as how to read in the array.
Asked
Active
Viewed 251 times
1 Answers
5
I'm just confused as to what my Map.cpp file is supposed to look like.
- First of all, you can not write your template class implementation in a .cpp file. It should be all in a header file. Read the following for more info. Why can templates only be implemented in the header file?
- Secondly, there is no constructor declaration in your
Map
class which takesstd::string
as a parameter. Provide one!template <typename Domain, typename Range> class Map { public: Map(const std::string& filename); // declare one constructor which takes std::string // ... other members };
Thirdly, your member function definitions missing the template parameters.
template <typename Domain, typename Range> // ---> this void Map<Domain, Range>::add(Domain d, Range r) { // implementation } template <typename Domain, typename Range> // ---> this bool Map<Domain, Range>::lookup(Domain d, Range& r) { // implementation }
- Last but not the least, you are missing a proper destructor, which is
essential for the
Map
class, as the allocated memory(usingnew
) should be freed. Therefore, do accordingly the The rule of three/five/zero.
That being said, if you could have used std::vector
, the manual memory management could be avoided.
#include <vector>
template <typename Domain, typename Range>
class Map
{
public:
//...
private:
// other members
std::vector<Domain> dArray;
std::vector<Range> rArray;
};
As a side note, avoid practising with using namespace std;
.
Why is "using namespace std;" considered bad practice?

JeJo
- 30,635
- 6
- 49
- 88
-
24th: Why `new[]`? Why not `std::vector`? ;-) [SO: new int\[size\] vs std::vector](https://stackoverflow.com/a/9741542/7478597) – Scheff's Cat Sep 18 '19 at 05:19
-
1