地图 在flex C ++中无法编译

问题描述 投票:2回答:1

我在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++ dictionary flex-lexer
1个回答
3
投票

生成的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"));
%% 
© www.soinside.com 2019 - 2024. All rights reserved.