C++ 使用STL容器作为成员时,cpprestsdk处理程序类崩溃
我使用cpprestsdk编写了一个简单的代码。我使用映射(C++ 使用STL容器作为成员时,cpprestsdk处理程序类崩溃,c++,cpprest-sdk,C++,Cpprest Sdk,我使用cpprestsdk编写了一个简单的代码。我使用映射(记录)作为CommandHandler类的成员,并在公共方法(has_record())中对其进行操作 它在handler.open().wait()运行之前工作,但当我在请求中调用它时,它崩溃了 这是我的密码: #define BOOST_DATE_TIME_NO_LIB #include <string> #include <vector> #include <cpprest/uri.h> #i
记录
)作为CommandHandler类的成员,并在公共方法(has_record()
)中对其进行操作
它在handler.open().wait()
运行之前工作,但当我在请求中调用它时,它崩溃了
这是我的密码:
#define BOOST_DATE_TIME_NO_LIB
#include <string>
#include <vector>
#include <cpprest/uri.h>
#include <cpprest/http_listener.h>
#include <cpprest/asyncrt_utils.h>
using namespace std;
using namespace web;
using namespace http;
using namespace utility;
using namespace http::experimental::listener;
class CommandHandler
{
public:
CommandHandler(utility::string_t url);
pplx::task<void> open() { return m_listener.open(); }
pplx::task<void> close() { return m_listener.close(); }
bool has_record();
private:
std::map< std::string, unsigned int > records;
void handle_get_or_post(http_request message);
http_listener m_listener;
};
bool CommandHandler::has_record()
{
return records.size() > 0 && records.find("1") != records.end();
}
CommandHandler::CommandHandler(utility::string_t url) : m_listener(url)
{
m_listener.support(methods::GET, std::bind(&CommandHandler::handle_get_or_post, this, std::placeholders::_1));
m_listener.support(methods::POST, std::bind(&CommandHandler::handle_get_or_post, this, std::placeholders::_1));
}
void CommandHandler::handle_get_or_post(http_request request)
{
if(this->has_record())
request.reply(status_codes::OK, 1);
else
request.reply(status_codes::OK, 0);
};
int main(int argc, char** argv)
{
try
{
utility::string_t address = U("http://127.0.0.1:9595");
uri_builder uri(address);
auto addr = uri.to_uri().to_string();
CommandHandler handler(addr);
if(handler.has_record())
std::cout<<"work!";
handler.open().wait();
ucout << utility::string_t(U("Listening for requests at: ")) << addr << std::endl;
ucout << U("Press ENTER key to quit...") << std::endl;
std::string line;
std::getline(std::cin, line);
handler.close().wait();
}
catch (std::exception& ex)
{
ucout << U("Exception: ") << ex.what() << std::endl;
ucout << U("Press ENTER key to quit...") << std::endl;
std::string line;
std::getline(std::cin, line);
}
return 0;
}
#定义BOOST_DATE_TIME_NO_LIB
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
使用命名空间web;
使用名称空间http;
使用名称空间实用程序;
使用名称空间http::experimental::listener;
类命令处理程序
{
公众:
CommandHandler(实用工具::字符串url);
pplx::task open(){return m_listener.open();}
pplx::task close(){return m_listener.close();}
布尔有记录();
私人:
std::map记录;
无效句柄\u get\u或\u post(http\u请求消息);
http_侦听器m_侦听器;
};
bool CommandHandler::has_record()
{
return records.size()>0&&records.find(“1”)!=records.end();
}
CommandHandler::CommandHandler(实用工具::字符串url):m_侦听器(url)
{
支持(方法::GET,std::bind(&CommandHandler::handle_GET_或_post,this,std::占位符::_1));
支持(methods::POST,std::bind(&CommandHandler::handle\u get\u或_POST,this,std::placeholders:::\u 1));
}
void CommandHandler::handle\u get\u或\u post(http\u请求)
{
如果(此->有\u记录())
请求.答复(状态代码::OK,1);
其他的
请求.答复(状态代码::OK,0);
};
int main(int argc,字符**argv)
{
尝试
{
实用工具::字符串\U t地址=U(“http://127.0.0.1:9595");
uri_生成器uri(地址);
auto addr=uri.to_uri().to_string();
命令处理程序(addr);
if(handler.has_record())
std::cout我发现了问题,但我不理解它抛出的原因。问题在这行代码中:
request.reply(status_codes::OK, 0);
0
引发异常