mooly mooly - 7 days ago 6
C Question

map<char[], char[]> in flex c doesnt compile

I am working on flex (.lex file) in vmPlayer on linux, and I want to convert sass code to css code.
I want to work with a map of char arrays, to match variables in sass to their values. For some reason, I can not insert values to my map.

%{
#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
%%


it does not compile and gives me error:

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"));


In general, I am not sure what C libraries I can use easily on flex and which are more fishy using flex.
Is there a syntax problem ? THANK YOU!

Answer

The issue in the generated C++ code is that pair(const char [3], const char [5]) (which is the size of your constant strings) has nothing to do with pair(const char [20], const char [20]). It is just not the same type.

3 solutions:

  • add template arguments for char array sizes
  • use char [] instead if you only have constants to insert
  • or better & simpler & covers all cases: use std::string type, which accepts char arrays in its constructor.

like this:

%{
   #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"));
%% 
Comments