g++ error in compiling while using std::string[5] as a type in std::map<>

Issue

I am fairly new to c++, I was making a encryptor to imrpove my c++, at first I kept my Cryptographer class in cryptographer.hpp and then added function body in cryptographer.cpp and then included cryptographer.hpp in main.cpp it gave me a compiler error, so I just pasted the code in main.cpp like this

#include <iostream>
#include <map>

class Cryptographer{
    public:
        int n_factor;
        std::string text;
        Cryptographer(std::string user_arg, int user_n_factor);
        struct cryptographer
        {
            std::string encrypted_text;
            std::string generated_key="";
        };
        cryptographer crypted_text;
        void generate_key();
        void encrypt();
        void decrypt();
        std::string get_key();
        std::string get_text();
};

using key_map = std::map<char, std::string[5]>;

void Cryptographer::generate_key(){
    for (int _ = 0; _ < 5; _++){
        crypted_text.generated_key += rand() % 26 + 65;
    }
}

void Cryptographer::encrypt(){
    generate_key();
    key_map keyHashMap;
    for (auto key_letter: crypted_text.generated_key){
        int key_letter_int = (int) key_letter;
        std::string key_letter_arr[5];
        int memory_number = key_letter_int;
        
        for (int index=0; index < 5; index++){
            if (memory_number+n_factor > 91){
                memory_number = 65;
            }else{
             key_letter_arr[5] = std::string(1, char (memory_number + n_factor));
             memory_number += n_factor;   
            }
        }
        keyHashMap.emplace(key_letter, key_letter_arr);
    }
    for(int index=0; index<text.size(); index++){
        int key = index %4;
        int key_patter = rand()% 4;
        int checking_index = 0;
        for (auto &elem: keyHashMap){
            if (checking_index == key){
                std::cout << elem.second[1];
            }
        }
    }
    crypted_text.encrypted_text = "test";
}

Cryptographer::Cryptographer(std::string user_arg, int user_n_factor):
    text(user_arg), n_factor(user_n_factor)
{}



int main(){
    Cryptographer crypter("hello guys", 3);
    crypter.encrypt();
    std::cout << crypter.get_text();
    return 0;
}

and ran this in my terminal

g++ main.cpp -o test
and it popped this large error
https://hastebin.com/cibeyanoro.cpp

I am on ubuntu 20.04, I also tried removing and reinstalling latest version of g++ but the same error pops up.

Solution

g++ error in compiling while using std::string[5] as a type in std::map<>

Arrays cannot be stored as elements of std::map. You can store classes though, and arrays can be members of a class. The standard library provides a template for such array wrapper. It’s called std::array. You can use that as the element of the map instead.

Sidenote: std::map isn’t the only standard container with such limitation. std::array is the only standard container that can itself contain arrays as elements.

Answered By – eerorika

Answer Checked By – David Marino (AngularFixing Volunteer)

Leave a Reply

Your email address will not be published.