C++ vector发生了什么<;字符串>;阵列?

C++ vector发生了什么<;字符串>;阵列?,c++,arrays,vector,C++,Arrays,Vector,我正在编写一个lex扫描器,其中我定义了一个数组,如下所示: // array of vector<string> std::vector<std::string> Lexicals[5] = { // [0] OPERATORS (pre-initialized with values) {"...", "..."}, // [1] PUNCTUATIVES (pre-initialized with values) {"...",

我正在编写一个lex扫描器,其中我定义了一个数组,如下所示:

// array of vector<string>
std::vector<std::string> Lexicals[5] = {
    //  [0] OPERATORS (pre-initialized with values)
    {"...", "..."},
    //  [1] PUNCTUATIVES (pre-initialized with values)
    {"...", "..."},
    //  [2] KEYWORDS (pre-initialized with values)
    {"...", "..."},
    //  [3] IDENTIFIERS  - add as found
    std::vector<std::string>(),
    //  [4] LITERALS  - add as found
    std::vector<std::string>()
};
导致问题的是
标识符
文本
选项。以下逻辑尝试检索正确的向量容器,并添加新值并标识位置或标识现有值的位置:

case LexType::IDENTIFIER:
case LexType::LITERAL: {
    string val(read_buffer(), m_length);
    //  Lexicals[3] || [4]
    vector<string> lex = Lexicals[m_lexType];
    vector<string>::iterator it;

    //  make sure value is not already in the vector
    if(!lex.empty()){
        it = find(lex.begin(), lex.end(), val);
        if(it == lex.end()) {                    
            lex.push_back(val);
            it = std::find(lex.begin(), lex.end(), val);
        }
    } else {                
        lex.push_back(val);
        it = find(lex.begin(), lex.end(), val);
    }

    m_lexical = it - lex.begin();
}
break;
case LexType::标识符:
大小写LexType::LITERAL:{
字符串val(read_buffer(),m_length);
//词汇[3]| |[4]
向量lex=词汇[m_lexType];
向量::迭代器;
//确保值不在向量中
如果(!lex.empty()){
it=find(lex.begin(),lex.end(),val);
如果(it==lex.end()){
lex.推回(val);
it=std::find(lex.begin(),lex.end(),val);
}
}否则{
lex.推回(val);
it=find(lex.begin(),lex.end(),val);
}
m_lexical=it-lex.begin();
}
打破

在第一遍之后的每次迭代中,
!lex.empty()
被忽略。我只是想弄清楚发生了什么。

问题很可能是这一行:

vector<string> lex = Lexicals[m_lexType];

尽管我现在遇到了一个错误:
D:\RzPlus/token.cpp:16:对
Lexicals'`的多个定义。词汇在包含的标题中声明。我正在从C++和Python中跳转到C++,所以我正在学习大的教训。@ IAbstract声明和定义之间有区别。在头文件中,您应该声明数组(例如,
extern std::vector Lexicals[5];
),然后在单个sourcel文件(也称为)中,您可以像在显示的代码中一样定义数组。似乎我还必须将声明放在命名空间中:/…现在可以工作了,谢谢!!!
vector<string> lex = Lexicals[m_lexType];
vector<string>& lex = Lexicals[m_lexType];