I need to read a json file with jsoncpp library.
I have this file:
{"one":false,"two":[{"id":"first"},{"id":"second"}],"three":550}
If i need to read only the first id of "two" element, i use:
std::string contents; //Contain the file
Json::Value root;
Json::Reader reader;
reader.parse(contents, root, false);
std::string aux = root["two"][0]["id"].asString();
It works fine in Linux, but when i try it in Windows i have this error:
error: ambiguous overload for 'operator[]' in 'root.Json::Value::operator[](((const char*)"two"))[0]'
Why it happens and how can i fix this? Thanks.
Solved: There are two operator[], one with an int as parameter and other with a const char as parameter, and the compiler doesn't know who to use in Windows, but yes in Linux. Now i use [0u] instead [0] to indicate a number as parameter and it works fine.
This drove me crazy, until I stumbled upon this question, I mysteriously figured it out:
Just cast it to Json::Uint (for some reason), or use 'u':
MapEvent::MapEvent(Json::Value& val)
{
operator_enum = val.get(0u, "").asString();
}
Related
I'm trying to pass a string argument to a function from the main function and then passing this received string argument to ifstream constructor. I'm able to receive this string in the function, but when I pass this argument to ifstream, I get a error message:
no matching function for call to ‘std::basic_ifstream::basic_ifstream(const string&)’
std::ifstream file(fileName);
Here is my code:
int** read_CSV(std::string const& fileName)
{
//cout<<fileName<<"\n";//this works
std::ifstream file(fileName);//problem
//Rest of logic
}
The main function:
int main()
{
int** inputMatrix1 = read_CSV("inputData4_80-20_100x32.csv");
return 0;
}
The variable fileName is creating the problem. If I pass it as it is, it gives the error. But instead, if I explicitly mention the name of the file using string rather than the variable, the code works fine. Can someone explain what exactly is the problem here and how I can solve it?
Okay, so the problem was that I was trying to compile without C++11 standard using just g++ "NameOfFile.cpp". There are 2 possible solutions to the problem, from the comment section, and they are as follows:
Using -std=c++11 when giving the command for compilation. The resultant compilation command would look as follows:
g++ -std=c++11 "NameOfFile.cpp"
Another solution, as pointed out by #Peter, is to use c_str() at the end of the string object, since the ifstream doesn't accept strings as an argument before c++11, so c_str() is used for explicitly converting to a compatible format.
Here's the modified code line for the second solution:
std::ifstream file(fileName.c_str());
Thanks to #Peter and #Holt for their inputs.
I have run into issues (runs on Intel, odd run results on ARM) when using this typedef:
typedef char IPString[17];
...
IPString ipStr;
extractIPfromURL("https://192.168.0.1:80", ipStr);
NOTE: I CANNOT use std::string, because code needs to compile on GCC and IAR. While IAR does support std::string the rule is not to use it.
If extractIPfromURL signature is:
void extractIPfromURL(const char* url, IPString *ipStr);
and implementation uses:
const char* ep;
...
strncpy(ipStr[0], &ep[start], end-start+1);
*ipStr[end+1] = '\0';
caller for pointer:
IPString ipStr;
extractIPfromURL("https://192.168.0.1:80", &ipStr);
everything works.
But if I was to use signature:
void extractIPfromURL(const char* url, IPString &ipStr);
and implementation:
const char* ep;
...
strncpy(&ipStr[0], &ep[start], end-start+1);
ipStr[end+1] = '\0';
caller for reference:
IPString ipStr;
extractIPfromURL("https://192.168.0.1:80", ipStr);
The code on ARM appears to behave as I rewrote some of the stack and my loop that is supposed to iterate over 2 items iterates over 2 items forever like: 0, 1, 0, 1, 0, 1, etc.
I tried an explicit reference declaration:
typedef char (&IPStringRef)[17];
but got the same loop on ARM (Raspberry PI).
I am sure I am not using typedef correctly in here, but I don't understand what exactly is it that I am doing wrong.
Your question is rather unclear. Part-way down the question you say you say "If [I do this] everything works". So the obvious solution is: do that.
I am assuming that you are asking why the code you posted bits of at the start of the question doesn't work.
Assuming that code is:
typedef char IPString[17];
void extractIPfromURL(const char* url, IPString *ipStr);
void some_function(void)
{
IPString ipStr;
extractIPfromURL("https://192.168.0.1:80", ipStr);
}
You should get a compilation error. If you don't see an error message then it is time to adjust the flags you pass to the compiler. The code is a constraint violation because the argument type char * does not match the parameter type char (*)[17].
The argument should match the parameter type. You have (at least) three different options for the argument: IPstring, IPstring *, IPstring &. They will all work , so long as you pass the matching argument form, and you use the parameter correctly inside the function (and your code doesn't contain any other bugs of course).
If you are still having trouble then try to post a MCVE. At the moment it is anybody's guess what is causing the problem you see on ARM since you only posted bits and pieces.
You could also consider not using the typedef at all.
This code is suspicious:
strncpy(&ipStr[0], &ep[start], end-start+1);
ipStr[end+1] = '\0';
If start is not zero then you are placing the \0 some distance past the end of where you actually finished copying characters. I would recommend avoiding strncpy entirely as it is relatively difficult both to use correctly, and to verify correct use of when you are reviewing code later.
so I'm a newbie at C++, and I've been poking around on the internet on how to do this, and so far I have this:
void includeFile(string name){
ifstream ifs;
ifs.open(name);
string commands;
while (getline(ifs,commands)){
commandReader(ifs);
}
ifs.close();
}
(commandReader is a function that takes an istream)
When I try to compile, I get the error "no matching function for call" and then gives me the line number for the ifs.open(name) line. I've included fstream, so not sure why it's doing this
Sorry, never mind; found the answer right after I posted this.
The solution was to have name.c_string() as the parameter instead, as string support was only added in c++11
As #chris pointed out, pre-C++11, ifs.open expects a char*, not an std::string. Try ifs.open(name.c_str()).
Basically I'm reading the contents of a file using fstream then converting it to const char* type. I'm supplying this to Lua, and Lua will do something with this. This however does not work. What does work is if I do:
const char* data = "print('Hello world')";
luaL_pushstring(L, data);
luaL_setglobal(L, "z");
They both are in the type const char* type and they are both the same string (e.g. I compared the two lengths). Except one works, and the other. I'm baffled. Any help here? Here is the code:
std::string line,text;
std::ifstream in("test.txt");
while(std::getline(in, line))
{
text += line;
}
const char* data = text.c_str();
luaL_pushstring(L, data);
luaL_setglobal(L, "z");
Here is the Lua code:
loadstring(z)()
To diagnose this, you probably want to know more about what Lua thought. I'd write the Lua side as assert(loadstring(s))() instead. If loadstring fails, your current code at best prints an error from the attempt to call nil. With the assert() in the sequence, the call to nil will be replaced by a more informative error about what went wrong.
Don't you have to set the global before you push the value? Anyways, what's up, Camoy :P
As the title says, is there any way to pass the path to the file / filename to open as an argument in the function?
I've written a short code for printing a .txt-file to the screen in C++, but instead of having all the code in the main(), I'd rather have it as an own function that I can call with the filename of the file to open as the only input argument.
Basically the beginning of the function would look like
void printFileToScreen()
{
ifstream fin;
char c;
fin.open("FILE_TO_OPEN.txt", ios::in);
blablabla
}
Now is there any way to pass "FILE_TO_OPEN.txt" when I call the function?
I've tried
void printFileToScreen(string str)
{
ifstream fin;
char c;
fin.open(str, ios::in);
blablabla
}
where I call the function like printFileToScreen("FILENAME.txt"), but with no luck, so I'm not sure how to do this.
Hope anyone can help :)
Unfortunately, the iostream functions deal with const char* types rather than with std::string (the iostream functions were developed independently of the STL). You instead could use std::string::c_str() to obtain a const char*:
fin.open(str.c_str(), ios::in);
As a general design rule, I would not pass the file name to the called function. I would pass the already opened std::istream object to read from. This allows you to do the job of printing in a function, and to do the job of opening the file and dealing with non-existent files in another. This has the bonus of being able to pass std::cin to your function!
Try changing your function to look like this :
void printFileToScreen(const string &str);
//If you pass a const char*, a string will be constructed
or this :
void printFileToScreen(const char *);
The function you wrote expects an instance of std::string to be passed by value.
Never mind, after some more trying and failing I found out that I needed to pass a char pointer, and not a string. :)
Of course you can pass the filename as a function parameter. If in doubt, pass a "const char*" rather than a string. I should work.