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

Incompatible Pointer Type #1196

Closed
weirdbeardgame opened this issue Aug 16, 2018 · 7 comments
Closed

Incompatible Pointer Type #1196

weirdbeardgame opened this issue Aug 16, 2018 · 7 comments
Labels
kind: question solution: proposed fix a fix for the issue has been proposed and waits for confirmation

Comments

@weirdbeardgame
Copy link

  • Attempting to read Json array "Item" : ["data", "data"]

charArray = ["Array"].get<char*>();

  • What is the expected behavior?
    It reads the array from the file into the char pointer array

  • And what is the actual behavior instead?
    Error C2338 incompatible pointer type

  • Which compiler and operating system are you using? Is it a [supported compiler]
    Visual Studio 2017

  • Did you use a released version of the library or the version from the develop branch? No

@nlohmann
Copy link
Owner

The library does not convert into pointers, but rather returns a pointer to the container used internally. Arrays are stored as std::vector<nlohmann::json> and strings are stored as std::string. Therefore, a char* could only be returned for a single string.

@weirdbeardgame
Copy link
Author

weirdbeardgame commented Aug 16, 2018

@nlohmann so then what would be the best way to read this char array out of the file with nlohman json and into a char array or is there one aside from reading it as a string I mean.

@nlohmann
Copy link
Owner

Can you give a precise example?

@weirdbeardgame
Copy link
Author

I mean I have a Json array of directions for a pet project text adventure game so "Directions" : ["N", "S"] I want to read it into c++ char* directions so I could print on a per room basis valid directions are N S

@nlohmann
Copy link
Owner

OK. Assuming input.json has the content {"Directions": ["N", "S"]}

std::ifstream i("input.json");
json j = json::parse(i);
for (auto& el : j["Directions"])
{
   std::string dir1 = el; // access string copy
   std::string& dir2 = el.get_ref<std::string&>(); // access string ref
   std::string* dir3 = el.get_ptr<std::string*>(); // access string ptr
   const char* dir4 = el.get_ptr<std::string*>()->c_str();
}

I haven't tested the code. but you should get the idea.

Docs:

@nlohmann nlohmann added kind: question solution: proposed fix a fix for the issue has been proposed and waits for confirmation labels Aug 16, 2018
@nlohmann
Copy link
Owner

@kenshen112 Do you need further assistance?

@weirdbeardgame
Copy link
Author

The purposed solution worked

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind: question solution: proposed fix a fix for the issue has been proposed and waits for confirmation
Projects
None yet
Development

No branches or pull requests

2 participants