I am having trouble resolving this error. I am not having any luck on Google when I search for this error.
no suitable constructor exists to convert from "int" to "std::pair<int, int>"
#include <utility>
using namespace std;
pair<int, int> solve(int s, int g)
{
return s % g != 0 ? (-1, -1) : (g, s - g);
}
The error squiggle is under the first s in the return where it is checking...
s % g != 0
I cannot figure out how to resolve this. In C# this would work.
public static (int, int) solve(int s, int g) => s % g != 0 ? (-1, -1) : (g, s - g);
(a, b)
is not a pair, it's an expression using the comma operator. It evaluates both the a
and the b
, but the result of the expression is the b
only. That's why it's complaining that it cannot convert your single int
into a pair
.
For example:
d = (a++, b+=3, c);
will:
a
;b
; andd
to whatever value c
has.If you want an actual pair, you should be using something like std::make_pair(a, b)
. In your particular case, that would be something like:
return (s % g != 0) ? make_pair(-1, -1) : make_pair(g, s - g);