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;
}
It looks like what you are after is either a
static
memberIn Morse.h change
to
and in Morse.cpp add
Or a Member Initializer List
In Morse.cpp change
to
The first option,
static
, is likely the better one because it doesn't repeat theencodeMap
in every instance of MorseCode.Do the same with
decodeMap
.