比较从函数返回的两个std::string常量(两个json精神get_str())1-liner

comparing two std::string consts returned from functions (two json-spirit get_str()s) 1-liner

本文关键字:两个 精神 json 常量 get 1-liner str string 返回 函数 std      更新时间:2023-10-16

我正在使用json精神为我的ws-++服务器解析json。

我正在比较用于验证注册过程、已确认登录、电子邮件、密码等的字符串。

json精神的get_str()返回const std::string&

我试过==compare,甚至strcmp。当我试图将函数返回与"1 liner"(全部在if比较语句中)进行比较时,所有这些都会使程序与terminate called after throwing an instance of 'std::runtime_error' what(): value type is 0 not 2崩溃。

这个函数可以使用1行返回吗?

这里只是猜测:我认为您的源json并没有根据值的类型提供您所认为的内容。

我不熟悉json精神,但你的链接有这样一段话:

You obtain the Value's type by calling Value::type(). You can then call the appropriate getter function. Generally, you will know a file's format, so you will know what type the JSON values should have. A std::runtime_error exception is thrown if you try to get a value of the wrong type, for example, if you try to extract a string from a value containing an integer.

这听起来很像你看到的错误。我猜json精神认为你的字符串根本不是字符串。当您尝试对非字符串(定义为类型"0"的内容)调用get_str()时,它会引发异常。

EDIT:在json精神的源代码中,类型"0"是NULL_type