Negating problems

I read the part about the negate function on this website and since most of it i don't understand how to read i need a little help understanding what's wrong with my code.

1
2
3
4
5
if (b1 > b2){
   b3 = b1 - b2;
   transform ( b3, b3+1, b3, negate<int>() );}
if (b2 > b1){
   b3 + b2 - b1;}


As you can see it finds the difference in two numbers and shows you the loss/gain kinda (in my project here)....

I run windows with Mingw compiler and it really doesn't like that line.

Help would be much appreciated.
negate? Thats not a standard function... Why do you need it? Do you have the library its in?
It's doesn't have a library as it seems and its part of the operator class in functional... its here in this website's reference: http://www.cplusplus.com/reference/std/functional/negate/

And i would need it just to make a number negative, its such a simple process... so do you know any other ways....
Last edited on
Yes, very simple. But first, the reason why negate isn't working is because you aren't putting any number into the ().

 
negate<int>(15);


Simple negation:
1
2
3
4
5
6
7
int makeNegative(int num)
{
    if (num>0)
    num *= -1;

    return num;
}
So, you mean like taking 5, and turning it into -5?

Multiply by -1

Edit: if you need to gurentee it is negative, check to make sure it is positive before multiplying by -1
Last edited on
Thank you very much i do feel like an idiot not remembering how to do that lol..... thanks anyways!
closed account (DSLq5Di1)
@gold
I think your confusion stems from trying to use a function object (std::negate) for a single value, rather than in an algorithm on a range of values as it is designed for. http://www.cplusplus.com/reference/algorithm/

As for negating a single value, its as simple as placing a minus sign before it.

1
2
3
4
inline int Negate(int n)
{
    return -n;
}
Topic archived. No new replies allowed.