C++ How to implement an unordered_map in .cpp that is instantiated in a header?

2.3k views Asked by At

I am working on a project where I must use unordered_maps to create definitions for morse code and then use them to translate to and from morse code. I am at a complete loss as to how in the world i'm supposed to fill a privately declared unordered_map from a header in the .cpp file.

Using private member functions across files has been an incredible headache in c++ for me and no amount of research has made it at all clear how it actually works so any advice is very welcome. Thanks.

Here's my code.

Morse.h:

#ifndef _MORSE_H
#define _MORSE_H 3710201612

#define MORSE_SET 45

#include <string>
#include <unordered_map>

using namespace std;

class MorseCode
{
  public:
    MorseCode ();

    string enCode(const char&) const;
    char   deCode(const string&) const;

  private:
    unordered_map<char,string> encodeMap;
    unordered_map<string,char> decodeMap;
};

#endif

Morse.cpp:

#include <iostream>
#include <string>
#include <unordered_map>

#include "Morse.h"
using namespace std;

MorseCode::MorseCode()
{
}

string MorseCode::encodeMap
 {
    { 'A', ".-" }, { 'B', "-..." }, { 'C', "-.-." }, { 'D', "-.." }, { 'E', "." },
    { 'F', "..-." }, { 'G', "--." }, { 'H', "...." }, { 'I', ".." }, { 'J', ".---" }, { 'K', "-.-" },
    { 'L', ".-.." }, { 'M', "--" }, { 'N', "-." }, { 'O', "---" }, { 'P', ".--." }, { 'Q', "--.-" },
    { 'R', ".-." }, { 'S', "..." }, { 'T', "-" }, { 'U', "..-" }, { 'V', "...-" }, { 'W', ".--" },
    { 'X', "-..-" }, { 'Y', "-.--" }, { 'Z', "--.." }, { '1', ".----" }, { '2', "..---" }, { '3', "...--" }, { '4', "....-" }, 
    { '5', "....." }, { '6', "-...." }, { '7', "--..." }, { '8', "---.." }, { '9', "----." }, { '0', "-----" },
    { '.', ".-.-.-" }, { ',', "--..--" }, { ':', "---..." }, { '?', "..--.." }, { '-', "-...-" },
    { '/', "-..-." }
};

char MorseCode::deCode(const string &) const
{
    return 0;
}
1

There are 1 answers

2
user4581301 On

It looks like what you are after is either a static member

In Morse.h change

unordered_map<char,string> encodeMap;

to

static unordered_map<char,string> encodeMap;

and in Morse.cpp add

unordered_map<char,string> MorseCode::encodeMap{
    { 'A', ".-" }, { 'B', "-..." }, { 'C', "-.-." }, { 'D', "-.." }, { 'E', "." },
    { 'F', "..-." }, { 'G', "--." }, { 'H', "...." }, { 'I', ".." }, { 'J', ".---" }, { 'K', "-.-" },
    { 'L', ".-.." }, { 'M', "--" }, { 'N', "-." }, { 'O', "---" }, { 'P', ".--." }, { 'Q', "--.-" },
    { 'R', ".-." }, { 'S', "..." }, { 'T', "-" }, { 'U', "..-" }, { 'V', "...-" }, { 'W', ".--" },
    { 'X', "-..-" }, { 'Y', "-.--" }, { 'Z', "--.." }, { '1', ".----" }, { '2', "..---" }, { '3', "...--" }, { '4', "....-" }, 
    { '5', "....." }, { '6', "-...." }, { '7', "--..." }, { '8', "---.." }, { '9', "----." }, { '0', "-----" },
    { '.', ".-.-.-" }, { ',', "--..--" }, { ':', "---..." }, { '?', "..--.." }, { '-', "-...-" },
    { '/', "-..-." }
};

Or a Member Initializer List

In Morse.cpp change

MorseCode::MorseCode()
{
}

to

MorseCode::MorseCode(): encodeMap {
           { 'A', ".-" }, { 'B', "-..." }, { 'C', "-.-." }, { 'D', "-.." }, { 'E', "." },
           { 'F', "..-." }, { 'G', "--." }, { 'H', "...." }, { 'I', ".." }, { 'J', ".---" }, { 'K', "-.-" },
           { 'L', ".-.." }, { 'M', "--" }, { 'N', "-." }, { 'O', "---" }, { 'P', ".--." }, { 'Q', "--.-" },
           { 'R', ".-." }, { 'S', "..." }, { 'T', "-" }, { 'U', "..-" }, { 'V', "...-" }, { 'W', ".--" },
           { 'X', "-..-" }, { 'Y', "-.--" }, { 'Z', "--.." }, { '1', ".----" }, { '2', "..---" }, { '3', "...--" }, { '4', "....-" },
           { '5', "....." }, { '6', "-...." }, { '7', "--..." }, { '8', "---.." }, { '9', "----." }, { '0', "-----" },
           { '.', ".-.-.-" }, { ',', "--..--" }, { ':', "---..." }, { '?', "..--.." }, { '-', "-...-" },
           { '/', "-..-." }
       }
{
}

The first option, static, is likely the better one because it doesn't repeat the encodeMap in every instance of MorseCode.

Do the same with decodeMap.