0

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;
}
Bren Bailey
  • 11
  • 1
  • 1
  • 8
  • 1
    Either make `encodeMap` static, or initialize it in `MorseCode` constructor. – Igor Tandetnik Dec 11 '16 at 04:56
  • I am very confused as to the syntax of initializing encodeMap in MorseCode. I've tried doing MorseCode::encodeMap Do you mean to not initialize it in the private section of Morse.h? Is it not possible to populate encodeMap in my .cpp without making it static or initializing it elsewhere? – Bren Bailey Dec 11 '16 at 05:07
  • user4581301 answers your question nicely. Some unrelated hints: 1) https://stackoverflow.com/q/1452721/6646408 Do not use `using namespace std` in header files 2) `_MORSE_H` is a reserved name because it starts with an underscore and a capital letter. Safe rule of thumb: Do not use identifier names with double underscores or a leading underscore. 3) Assigning a value to the `_MORSE_H` include guard macro is superfluous. The only thing that matters is if a macro of that name is defined at all. If your goal was to ensure uniqueness you needed to add random characters to the macro name itself. – besc Dec 11 '16 at 09:46

1 Answers1

0

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.

user4581301
  • 33,082
  • 7
  • 33
  • 54
  • Ah! The syntax of "MorseCode::MorseCode(): encodeMap" was what was getting me! I couldn't find an example of how to fill an unordered_map from a header file anywhere! Thank you! This does lead me to another puzzle though. What is the use of this code? Also apologies I can't figure out how to "codify" this text without the handy toolbox that's available when posting questions. string enCode(const char&) const; char deCode(const string&) const; – Bren Bailey Dec 11 '16 at 05:17
  • @BrenBailey Best you can do with codifying is use the ` accent that sits on most North American English keyboards with the ~. And even then the code will all be smushed onto one line. Those are declarations of methods of `MorseCode`. You will need to implement those methods. Your textbook should be able to explain this better than I can in a comment. [And if it can't, this one can.](https://www.amazon.com/dp/0321992784/?tag=stackoverfl08-20) You may also find what you need in a much abreviated fashion here: https://isocpp.org/tour – user4581301 Dec 11 '16 at 05:39