error_code 的格式说明符是什么?
What is the format specifier of error_code?
我正在尝试使用 Microsoft 的 cpprestsdk。我遇到了一个错误,所以我想检查错误代码。但是我无法弄清楚 error_code
的格式说明符,并且收到此警告:
warning: format ‘%d’ expects argument of type ‘int’, but argument 3
has type ‘const std::error_code’ [-Wformat=]
printf("HTTP Exception :: %s\nCode :: %d\n", e.what(), e.error_code());
我应该如何打印错误代码?虽然 %d
有效,但我想知道实际的说明符,这样我就不会收到任何警告。
PS:我在这里看到了其中的一些:https://msdn.microsoft.com/en-us/library/75w45ekt(v=vs.120).aspx,但我认为它们对我没有任何帮助。
std::error_code
是一个 class 并且不能作为 printf 参数传递。但是您可以传递 error_code::value()
.
返回的 int
值
这是一种方法:
#include <system_error>
#include <cstdio>
void emit(std::error_code ec)
{
std::printf("error number: %d : message : %s : category : %s", ec.value(), ec.message(), ec.category().name());
}
但是我们不要使用 printf...
#include <system_error>
#include <iostream>
void emit(std::error_code ec)
{
std::cout << "error number : " << ec.value()
<< " : message : " << ec.message()
<< " : category : " << ec.category().name()
<< '\n';
}
我正在尝试使用 Microsoft 的 cpprestsdk。我遇到了一个错误,所以我想检查错误代码。但是我无法弄清楚 error_code
的格式说明符,并且收到此警告:
warning: format ‘%d’ expects argument of type ‘int’, but argument 3 has type ‘const std::error_code’ [-Wformat=] printf("HTTP Exception :: %s\nCode :: %d\n", e.what(), e.error_code());
我应该如何打印错误代码?虽然 %d
有效,但我想知道实际的说明符,这样我就不会收到任何警告。
PS:我在这里看到了其中的一些:https://msdn.microsoft.com/en-us/library/75w45ekt(v=vs.120).aspx,但我认为它们对我没有任何帮助。
std::error_code
是一个 class 并且不能作为 printf 参数传递。但是您可以传递 error_code::value()
.
int
值
这是一种方法:
#include <system_error>
#include <cstdio>
void emit(std::error_code ec)
{
std::printf("error number: %d : message : %s : category : %s", ec.value(), ec.message(), ec.category().name());
}
但是我们不要使用 printf...
#include <system_error>
#include <iostream>
void emit(std::error_code ec)
{
std::cout << "error number : " << ec.value()
<< " : message : " << ec.message()
<< " : category : " << ec.category().name()
<< '\n';
}