0

I'm trying to use Microsoft's cpprestsdk. And I was getting an error, and so I wanted to check the error code. But I'm not able to figure out the format specifier of error_code, and I'm getting this warning :

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());

How should I print the error code? Although %d works but I wanted to know the actual specifier so that I won't get any warnings.

PS: I saw some of them here : https://msdn.microsoft.com/en-us/library/75w45ekt(v=vs.120).aspx , but I don't think any of them are any help to me.

Nikhil Wagh
  • 1,376
  • 1
  • 24
  • 44
  • 3
    [`std::error_code`](http://en.cppreference.com/w/cpp/error/error_code) is a *class*. Of course you can't use it in the old C `printf` function. What is the reason you use `printf` to begin with? – Some programmer dude Mar 23 '18 at 14:05
  • Nothing, I was just wondering Is there a way to do it this way. – Nikhil Wagh Mar 23 '18 at 14:15

2 Answers2

3

std::error_code is a class and can not be passed as printf argument. But you can pass an int value returned by error_code::value().

user7860670
  • 35,849
  • 4
  • 58
  • 84
2

here's one way:

#include <system_error>
#include <cstdio>

void emit(std::error_code ec)
{
    std::printf("error number: %d : message : %s : category : %s", ec.value(), ec.message().c_str(), ec.category().name());
}

But let's not use 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';
}
Richard Hodges
  • 68,278
  • 7
  • 90
  • 142