flex C++ 中的 map<char[], char[]> 无法编译

map<char[], char[]> in flex C++ doesn't compile

我正在 linux 上的 vmPlayer 中处理 flex(.lex 文件),我想将 sass 代码转换为 css 代码。 我想使用 char 数组映射,将 sass 中的变量与其值匹配。出于某种原因,我无法在我的地图中插入值。

%{
   #include <stdio.h>
   #include <stdlib.h>
   #include <string>
   #include <map>
   #include<iostream>
   std::map<char[20], char[20]> dictionary;   //MY DICTIONARY,GOOD
%}
%%
s       dictionary.insert(std::pair<char[20], char[20]>("bb", "TTTT")); //PROBLEM
%% 

它没有编译并给我错误:

hello.lex:30:84: error: no matching function for call to ‘std::pair<char    
[20], char [20]>::pair(const char [3], const char [5])’
ine(toReturn);  dictionary.insert(std::pair<char[20], char[20]>("bb", 
"TTTT"));

一般来说,我不确定哪些 C 库可以在 flex 上轻松使用,哪些使用 flex 更可疑。 有语法问题吗?

生成的 C++ 代码中的问题是 pair(const char [3], const char [5])(这是常量字符串的大小)与 pair(const char [20], const char [20]) 无关。只是不是同一类型

3 个解决方案:

  • 为 char 数组大小添加模板参数(编辑:不起作用,因为所有元素的大小仍然必须相同)
  • 如果您只有要插入的常量,请改用 char []
  • 或更好、更简单并涵盖所有情况:使用 std::string 类型,它在其构造函数中接受 char 数组。

像这样:

%{
   #include <stdio.h>
   #include <stdlib.h>
   #include <string>
   #include <map>
   #include<iostream>
   std::map<std::string, std::string> dictionary;   //MY DICTIONARY,GOOD
%}
%%
s       dictionary.insert(std::pair<std::string, std::string>("bb", "TTTT"));
%%