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

Unclear error message: "cannot use operator[] with a string argument with array" #2047

Closed
nickwebha opened this issue Apr 17, 2020 · 1 comment

Comments

@nickwebha
Copy link

nickwebha commented Apr 17, 2020

  • Describe what you want to achieve.
    Send array of arrays over a network connection.

  • Describe what you tried.
    On the server:

nlohmann::json jsonMessageOut;
jsonMessageOut[ "action" ] = "updateUnits";

databaseResults R = database.query( "SELECT \"longitude\", \"latitude\" FROM \"units.lonlat\" LIMIT 10;" );
if ( R.size() > 0 ) {
	std::vector< std::pair< double, double > > units;
	for ( const auto& unit : R )
		units.push_back( { unit[ 0 ].as< double >(), unit[ 1 ].as< double >() } );

	jsonMessageOut[ "units" ] = units;
}
else
	jsonMessageOut[ "units" ] = nlohmann::json::array();

[send]

On the client:

while ( ! ( jsonMessageIn = websocketGetMessageInbound() ).empty() ) {	// TODO Look into pushing these into the SDL event queue.
	const std::string action = jsonMessageIn[ "action" ];

	switch ( networkStringValues[ action ] ) {
		...

		case networkActionUpdateUnits: {
			std::vector< std::pair< double, double > > units = jsonMessageIn[ "units" ];

			for ( const auto& unit : units )
				std::cout << unit.first << "x" << unit.second << std::endl;
		}

		...
	}
}
  • Describe what happens instead.
    [[98.02922343358784,-71.97847384808085],[-63.31191659209324,-35.374567995631736],[-110.474999892986,-6.113705884129388],[69.75989939459609,67.16343799507328],[2.3197853267517132,81.05544938056028],[40.16905596844069,80.41366385334712],[-87.83292780117719,-7.74048656814756],[-93.33958934205057,-76.02122165690062],[-23.417427851405677,51.76305532126719],[32.21544673426408,83.85006816965088]] [json.exception.type_error.305] cannot use operator[] with a string argument with array

It seems strange to me that I can see the correct response in the error message but not access it like I would any other array.

  • Describe which system (OS, compiler) you are using.
    Emscripten v1.39.10/Clang v8.0.0-3

  • Describe which version of the library you are using (release version, develop branch).
    Release v3.7.3

@nickwebha
Copy link
Author

It was my mistake (I was sending the array directly, not the array as part of a larger object). I was missing about two lines of code. Sorry about that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant