Home c++ Max error in C++

Max error in C++




recently stumbled upon the problem in C++. There is an int mx variable and there is a set of SET VALS, you need to transfer the MX variable to the Max value (MX, VALS.SIZE ()), but Max breaks down. Tell me how to wipe the problem please.

int mx = 0;
set & lt; int & gt; VALS;
MX = MAX (valss.size (), MX);

Answer 1, Authority 100%

The fact is that max is a function template. Let’s look at the announcement of this template:

template & lt; Class T & GT;
constexpron const t & amp; max (Const t & amp; a, const t & amp; b);

As can be seen, two arguments have the same type. Now let’s look at your call Max :

max (valss.size (), mx);

So what is the problem? The problem is that valss.size () returns the value that the size_t type is. And MX has the type int . It turns out that the types are different and the compiler cannot determine the desired type. Therefore, you need to explicitly specify which type to use. To do this, simply specify the desired type in the angular brackets after the name of the function. Like this:

max & lt; int & gt; (valss.size (), mx);

Answer 2, Authority 80%

We need to translate MX in the std :: size_t >or initially it is so created.

std :: max (valss.size (), static_cast & lt; std :: size_t & gt; (mx));

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions