未在作用域中声明无序的_映射 我试图在C++中创建一个字母密码。我解决这个问题的方法是创建两个无序的映射。1字母及其在字母表中的相应位置,以及一个相对的表格

未在作用域中声明无序的_映射 我试图在C++中创建一个字母密码。我解决这个问题的方法是创建两个无序的映射。1字母及其在字母表中的相应位置,以及一个相对的表格,c++,unordered-map,C++,Unordered Map,当我尝试在函数encrypt中访问此无序映射时,收到一条错误消息: 映射未在此作用域中声明 我对C++还很陌生。起初,我尝试在main上方创建地图,但这似乎也不起作用 对于如何处理这种情况,有什么建议或提示吗 #include <iostream> #include <unordered_map> #include <string> #include <vector> #include <numeric> using namespace

当我尝试在函数encrypt中访问此无序映射时,收到一条错误消息:

映射未在此作用域中声明

我对C++还很陌生。起初,我尝试在main上方创建地图,但这似乎也不起作用

对于如何处理这种情况,有什么建议或提示吗

#include <iostream>
#include <unordered_map>
#include <string>
#include <vector>
#include <numeric>
using namespace std;

string enCrypt (string str, int x){ //encrypts the input letter with the offset variable (encryption key) x
    int pos = cipherMap.at(str);
    string encrypted;
    if (pos + x < 25){
        encrypted = alphaMap.at(pos + x);
        return encrypted;
    }else{
        pos = 25 - pos;
        encrypted = alphaMap.at(x - pos);
        return encrypted;
    }
}

int main()
{
    vector<string> alphabet(26);
    iota(alphabet.begin(), alphabet.end(), 'A');

    unordered_map<string, int> cipherMap; //map containing the alphabet and the corresponding position of the letter in the alphabet
    for (int i = 0; i < 26; i++){
        cipherMap.insert( { alphabet[i], i });
    }

    unordered_map<int, string> alphaMap; //opposite of earlier mentioned map
    for (int i = 0; i < 26; i++){
        alphaMap.insert( { i , alphabet[i] });
    }

    cout << enCrypt("A", 3); //trying to encrypt letter A, output should be D


    return 0;
}

通过VKNs解释,我成功地解决了以下程序

#include <unordered_map>
#include <string>
#include <vector>
#include <numeric>
using namespace std;

struct Cipher{
    Cipher (vector<string> alphabet){
        for (int i = 0; i < 26; i++){
            cipherMap.insert( { alphabet[i], i });
        }
        for (int i = 0; i < 26; i++){
            alphaMap.insert( { i , alphabet[i] });
        }
    }
    string encrypt (string str, int x){
        int pos = cipherMap.at(str);
        string encrypted;
        if (pos + x < 25){
            encrypted = alphaMap.at(pos + x);
            return encrypted;
        }else{
            pos = 25 - pos;
            encrypted = alphaMap.at(x - pos);
            return encrypted;

        }
    }
private:
    unordered_map<string, int> cipherMap;
    unordered_map<int, string> alphaMap;
};

int main()
{

        vector<string> alphabet(26);
        iota(alphabet.begin(), alphabet.end(), 'A');

        Cipher cipher{alphabet};
        cout << cipher.encrypt("A", 3);
}
#包括
#包括
#包括
#包括
使用名称空间std;
结构密码{
密码(矢量字母表){
对于(int i=0;i<26;i++){
密码插入({字母[i],i});
}
对于(int i=0;i<26;i++){
插入({i,字母表[i]});
}
}
字符串加密(字符串str,int x){
int pos=低温下的温度(str);
字符串加密;
如果(位置+x<25){
加密=字母地图at(pos+x);
返回加密;
}否则{
pos=25-pos;
加密=字母地图at(x-pos);
返回加密;
}
}
私人:
无序映射密码;
无序的字母地图;
};
int main()
{
矢量字母表(26);
iota(alphabet.begin()、alphabet.end()、A');
密码{字母表};

cout这是您的
main
范围:

向量字母表(26)

无序映射密码

无序的字母地图

调用
string enCrypt(string str,int x)
时,函数可以在其入口点看到以下内容:

字符串str

int x

您没有向它传递任何一个
unordered_map
s,因此它不知道您在说什么。自上而下阅读函数:

int pos

有一个名为pos的变量,它保存一个整数

=在(str)处的低温

分配给我不知道的对象(我只知道
string str
int x
int pos

有几种方法可以解决这一问题,从传递对
无序映射的引用到函数,再到让类为您处理所有这些(这可能是更好的选择)。类似这样:

struct Cipher {
    Cipher(string alphabet)
    { //...initializes the unordered_maps
    }

    string encrypt(string str, int x)
    { //......
    }
private:
    unordered_map<string, int> cipherMap;
    unordered_map<int, string> alphaMap;
};

int main()
{
    string alphabet;
    //Initialize your alphabet and do whatever you must
    Cipher cipher{alphabet};
    string encrypted = cipher.encrypt(/* Some other string */);
}
struct密码{
密码(字符串字母表)
{//…初始化无序的_映射
}
字符串加密(字符串str,int x)
{ //......
}
私人:
无序映射密码;
无序的字母地图;
};
int main()
{
字符串字母表;
//初始化你的字母表,做任何你必须做的事情
密码{字母表};
字符串加密=cipher.encrypt(/*其他字符串*/);
}

通过这种方式,
encrypt
函数可以查看
Cipher
类的
unordered_map
成员变量,因为它也是
Cipher
的成员,而
alphaMap
main
的本地变量。错误消息说明了一切:变量未在使用它们的范围内声明。您是fam吗我熟悉定义范围的规则?这里有一些文档,尽管它可能更容易学习。感谢您的评论。通过您的帮助,我成功地解决了这个问题。非常感谢您花时间发表评论!我不知道数据结构存在,我真的很感谢您为帮助我所做的努力太多细节了!我设法让它工作了=)
struct Cipher {
    Cipher(string alphabet)
    { //...initializes the unordered_maps
    }

    string encrypt(string str, int x)
    { //......
    }
private:
    unordered_map<string, int> cipherMap;
    unordered_map<int, string> alphaMap;
};

int main()
{
    string alphabet;
    //Initialize your alphabet and do whatever you must
    Cipher cipher{alphabet};
    string encrypted = cipher.encrypt(/* Some other string */);
}