【发布时间】:2018-05-02 08:26:43
【问题描述】:
我在 C++ 中遇到了一个我不理解的问题。
这是我的代码:
auto DataArray = jvalue.at(U("data")).as_array();
std::cout << "Outside the loop, first output" << std::endl;
for (int i = 0; i <= 10; i++)
{
auto data = DataArray[i];
auto dataObj = data.as_object();
std::wcout << "inside the loop" << std::endl;
}
std::cout << "Outside the loop, second output" << std::endl;
输出:
Outside the loop, first output
inside the loop
inside the loop
inside the loop
inside the loop
inside the loop
inside the loop
inside the loop
inside the loop
inside the loop
inside the loop
Press any key to continue . . .
似乎代码在循环结束后停止了。 但为什么呢?
但是如果我注释掉了
//auto data = DataArray[i];
//auto dataObj = data.as_object();
没有问题。
顺便说一句,我正在处理 cpprest 并从 api 获取 json 对象数据。
jvalue 变量保存结果。
如果我尝试捕捉代码:
try {
auto data = DataArray[i];
auto dataObj = data.as_object();
std::wcout << "inside the loop" << std::endl;
}
catch (const std::exception& e) {
std::wcout << e.what() << std::endl;
}
结果是无限循环,输出:not an object。
请帮忙。谢谢。
【问题讨论】:
-
你确定
i <= 10? -
提示:您的循环定义为从
i=0运行到i=10(11 次),但"inside the loop"只打印了10 次。进行调查可能会让您了解正在发生的事情。
标签: c++ cpprest-sdk