Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Weird things on for statement #3447

Closed
2 tasks
EG-Julien opened this issue Apr 19, 2022 · 2 comments
Closed
2 tasks

Weird things on for statement #3447

EG-Julien opened this issue Apr 19, 2022 · 2 comments
Labels
kind: bug solution: proposed fix a fix for the issue has been proposed and waits for confirmation

Comments

@EG-Julien
Copy link

EG-Julien commented Apr 19, 2022

Description

Hi,
I'm using the library to read a kind of book database. The bug occurs when I try to "parse" all the records.

Reproduction steps

When I do a for statement on the DB get through a method it doesn't seems to get it properly. However when I declare a variable (auto type) and call it in the for statement it works as expected.

Expected vs. actual results

Trying to parse all id :
It gives me this when I declare a variable :

$ SEARCH aze
0
1
2
3
4
5
6
7
8

And gives me nothing when I put the getter in the for statement.

Minimal code example

auto db_json = this->db.getRawDatabase().at("records");
for (auto r : db_json) {
    cout << r.at("id") << endl;
}

Output :
0
1
2
3
...

with getRawDatabase() :

class Database
{
    private:
       nlohmann::json database;
       ...
   public:
      Database(const std::string __file);
      Database();
      ...
      nlohmann::json getRawDatabase() { return this->database; }
      ...

However this output nothing :

for (auto r : this->db.getRawDatabase().at("records")) {
    cout << r.at("id") << endl;
}


### Error messages

```Shell
No error.

Compiler and operating system

MacOS X 12.3.1 - Apple Clang 13.1.6

Library version

latest

Validation

@falbrechtskirchinger
Copy link
Contributor

falbrechtskirchinger commented Apr 19, 2022

Just change your getRawDatabase() function to:

const nlohmann::json &getRawDatabase() const { return this->database; }

You're having basic issues with C++. There's nothing special related to this library here.

Edit: Also, I've ignored other issues with your code. For example, your loop variable should be a const reference (you're making unnecessary copies):

for (const auto &r : this->db.getRawDatabase().at("records")) {
    cout << r.at("id") << endl;
}

@EG-Julien
Copy link
Author

Thanks it works just fine with this change.
I'm actually a student in electronics and I use this lib for a school project in order to learn cpp ... I was pretty sure it was related to my code instead of the lib :)

Thanks for your help and tips !

@nlohmann nlohmann added the solution: proposed fix a fix for the issue has been proposed and waits for confirmation label Jul 23, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind: bug solution: proposed fix a fix for the issue has been proposed and waits for confirmation
Projects
None yet
Development

No branches or pull requests

3 participants