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

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?

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 (EDIT: does not work, because it would still have to be the same sizes for all elements)
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"));
%%

Related

Simple Class with Constructor throws an warning: ISO C++ forbids converting a string constant to 'char*' [duplicate]

This question already has answers here:
Conversion from string literal to char* is deprecated [duplicate]
(2 answers)
Closed 2 years ago.
So I'm building a Regex class, with a simple constructor:
regex.hpp
#ifndef REGEX_CUSTOM_CLASS
#define REGEX_CUSTOM_CLASS
#include <stdio.h>
using namespace std;
class Regex
{
private:
/* data */
public:
char *regex;
Regex(char str[]);
};
#endif // REGEX_CUSTOM_CLASS
regex.cpp
#include <iostream>
#include <list>
#include <assert.h>
#include <ctype.h>
#include <stdio.h>
#include "regex.hpp"
using namespace std;
Regex::Regex(char str[])
{
regex = str;
}
tools.cpp
#include <iostream>
#include <stdio.h>
#include "lib/regex/regex.cpp"
using namespace std;
int main() {
Regex my_regex("//");
cout << my_regex.regex << endl;
return 0;
}
But after I compile it to .exe file and run it, I get this error message:
warning: ISO C++ forbids converting a string constant to 'char*' Regex my_regex("//");
I think the problem is with the data types. What is the problem?
You cannot pass arrays by value. When you write:
Regex::Regex(char str[])
this actually is
Regex::Regex(char* str)
Moreover string literals are of type const char [N] (where N is length of the string including the null terminator) and when passed to functions they decay to const char*. Getting a char* from a const char* (pointer to constant char, not to be confused with constant pointer to char) would break const correctness.
Either use std::string as argument type or change it to const char*.

c++ Array Error: Expected an expression

I've just now come across an error using arrays that seems odd, I've searched the web but it would appear this is typically something people run into when they are dealing with multidimensional arrays. The error occurs when I attempt to call a function that uses an array as a parameter. Here is the code:
#include <iostream>
#include <string>
#include <iomanip>
#include "header.h"
#include <fstream>
using namespace std;
int main(){
string make, model, licensePlate, address, name, phoneNumber;
int year, messageCode;
char choice;
bool ready[10];
string phoneNumber[10], name[10], address[10], licensePlate[10], make[10], model[10];
int year[10];
initializeArray(year[]);
The error occurs within the brackets of year[], expected an expression. Thanks in advance!
I apologize for my poor code!
Having both variables of int and an array with the same name was a terrible idea, as you do not use the brackets to pass an array. I removed the brackets and the int year variable.
Here is the revised work:
bool ready[10];
string phoneNumber[10], name[10], address[10], licensePlate[10], make[10], model[10];
int year[10];
initializeArray(year);

C++ vector of array of strings?

I want to have a dynamic structure which I could iterate on, there will be unknown number of entries and known number of strings for each entry. I thought that vector of array of strings could be the way, however I get error while compiling this:
vector< array<string, 5> >
error: invalid use of incomplete type 'struct std::array<std::basic_string<char>, 5u>'
What am I doing wrong? and if this is kind of the way - how would I add/get values to/from this structure?
Did you include all these three headers?
#include <vector>
#include <array>
#include <string>
This compiles just fine:
#include <vector>
#include <array>
#include <string>
int main(int argc, char const *argv[])
{
std::vector<std::array<std::string, 5> > myVec;
return 0;
}

Unable to tokenize a string and pass to a vector <string>

I am experimenting with CGI in C++. I know that there are libraries which handle basic stuff, but in order to know whats going on under the hood i have been trying to parse the stdin using string datatype ---> tokenize using '= and &' then push_back into a vector. at the latter step, i am receiving segmentation fault. given below is the program where i am using cin>> to obtain user input and so on ..
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <vector>
#include <algorithm>
#include <string.h>
using namespace std;
int main()
{
vector <string> stuff(0);
vector<string>::iterator it;
char* bufferchar;
string buffer;
char str[size];
cout<<"Content-type: text/html\n\n"
<<"<html>"
<<"<head>"
<<"<title>CGI SCRIPT</title>"
<<"</head>"
<<"<body>"
fgets(str,20,stdin); //20 is expect size of content from html form
puts(str);
cout<<"<p>Now to break this guy apart";
int x=0;
bufferchar = strtok(str,"&=");
buffer+=bufferchar;
stuff.push_back(buffer);
while(bufferchar!=NULL){
cout<<bufferchar<<'\n'<<'\n';
bufferchar=strtok(NULL,"&=");
buffer+=bufferchar;
stuff.push_back(buffer);
}
cout<<"<br>ok does the vector iterate ?";
for (it=stuff.begin();it!=stuff.end();++it){
cout<<*it;
cout<<"<br> ok man, next <br><br>";
}
cout<<"</body>";
cout<<"</html>";
}

Error declaring hash_map in lex file

I'm working on writing a simple preprocessor for a compiler. Below is an edited snippet of my code:
%{
#include <string.h>
#include <hash_map>
#include "scanner.h"
#include "errors.h"
struct eqstr {
bool operator()(const char* s1, const char* s2) const {
return strcmp(s1, s2) == 0;
}
};
std::hash_map<const char*, char*, hash<const char*>, eqstr> defs; // LINE 28
%}
// Definitions here
%%
// Patterns and actions here
%%
When I compile I get the following error:
dpp.l:28: error: expected constructor,
destructor, or type conversion before
β€˜<’ token
Any ideas what might be wrong with this? I pretty much copied and pasted this line from the sgi documentation.
You need std::hash rather than just hash, since you have no using statement that will pull it into scope. Also, the default std::hash<const char *> will hash the pointer directly, which won't work for this use -- you need a hash function that hashes the c-string pointed at. You need to define your own specialization of hash, or your own hashing function -- the latter is probably better.