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

more than one operator '=' matches these operands #1002

Closed
balldrix opened this issue Mar 9, 2018 · 4 comments
Closed

more than one operator '=' matches these operands #1002

balldrix opened this issue Mar 9, 2018 · 4 comments

Comments

@balldrix
Copy link

balldrix commented Mar 9, 2018

I'm copying data into a struct. I get this warning when I copy a std::string.

// read file
	std::ifstream file(fileName);

	// parse data from file
	json data = json::parse(file);
	json animation = data["animation"];

	// iterate through data and save in animation list
	for(json::iterator it = animation.begin(); it != animation.end(); ++it)
	{
		// json of current it in array
		json j = it.value();

		// data struct
		Animation animationType;
		animationType.name = j["name"];
		animationType.spriteSheetIndex = j["index"];
		animationType.frameCount = j["frameCount"];
		animationType.framesPerSecond = j["framesPerSecond"];
		animationType.loop = j["loop"];

		// push data to list
		m_animationList.push_back(animationType);
	}

The code works as intended but would you know why this message appears? Is there a better way for me to take the json file and put the data in a vector list?

I'm using Windows 7 & Windows 10, Visual Studio 2017 v15.5.6.

Using the most recent release version of nlohmann/json.

@theodelrieu
Copy link
Contributor

It's surprising that it even compiles. I would suggest using the get<> method:

animationType.name = j["name"].get<std::string>();

Also, you could refactor your code and create a from_json method for your struct Animation, you would then be able to do:

auto animations = data.get<std::vector<Animation>>();

@balldrix
Copy link
Author

balldrix commented Mar 9, 2018

Thank you, the get<> method works and has removed the warning.

I'm not sure what you mean about the from_json method. Could you explain?

@theodelrieu
Copy link
Contributor

You can take a look at this section of the readme.

If you need more info let me know

@balldrix
Copy link
Author

balldrix commented Mar 9, 2018

I created the from_json method and now I'm able to do the above with this:

// read file
std::ifstream file(fileName);

// parse data from file
json data = json::parse(file);
json animation = data["animation"];

m_animationList = animation.get<std::vector<Animation>>();`

@balldrix balldrix closed this as completed Mar 9, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants