-2

I'm trying to write some code that creates a class with the privates hours and minutes. Now I'm trying to create a new class from a integer minus a other class.

class Foo
{
Public:
    Foo(int u, int m);
    Foo(int m);
    int operator-(const Foo& other);
    friend Foo operator-(int lhs, const Foo& rhs);

Private:
    int minute, hour;
};

Foo::Foo(int u, int m): hour(u), minute(m){}
Foo::Foo(int m): hour(0), minute(m){}

int Foo::operator-(const Foo& other)
{
    int x;
    x = (60*(uur-other.uur));
    x += (min - other.min);
    return x;
}

main()
{
    Foo t1(2,10);
    const Foo kw(15);
    Foo t2(t1 -kw);
    Foo t3(2,10);
    Foo t4(132 -t3);
}

Now I can't get T4 to contain only 2 minutes (132 - ((60 * 2) -10)) Does anyone know how to solve this? I get the error: error: no match for 'operator-' (operand types are 'int' and 'Foo')

void operator-(int x, const Foo& other);

When I include this function i get the error error: 'void Foo::operator-(int, const Foo&)' must take either zero or one argument. Got it working with the following code:

Foo operator-(int lhs, const Foo& rhs) 
{ 
    int y; 
    y = lhs - rhs.min; 
    y -= (60 * rhs.uur); 
    return y; 
}
Dylan
  • 45
  • 1
  • 9
  • 3
    You need to [overload](http://stackoverflow.com/questions/4421706/operator-overloading) the `operator -` of the class. – NathanOliver May 15 '17 at 16:56
  • Or you could define a cast operator to convert a `Foo` to an `int`, and a `Foo` constructor that takes a single `int`. – Beta May 15 '17 at 17:07
  • `(1,0)` vs `(0, 60)`.. this is going to be fun. – Karoly Horvath May 15 '17 at 17:07
  • `Foo t4(132 -t3);` What do you expect this to do? In mathematics, the subtraction sign is typically used as a binary operator. You seem to be trying to use it as a unary operator. Do you intend for it to *negate* `t3`? What would that mean? Would it initialize `t4` with a negative number of minutes? – Cody Gray - on strike May 15 '17 at 17:12
  • I'm trying to initialize t4 as a integer minus a class. The integer will always be bigger then the class so t4 can't be negative. – Dylan May 15 '17 at 17:21
  • @Beta -- don't do that. It will cause all kinds of ambiguities. – Pete Becker May 15 '17 at 17:21
  • 1
    Try overloading the *non-member* function `Foo operator-(int lhs, const Foo &rhs)`. – G.M. May 15 '17 at 17:21

1 Answers1

2

As the error message says, you need an operator- that takes an int as its left-hand argument and a Foo as its right-hand argument. That can't be a member function, because member functions always take their own type as their first argument. So you have to make it a free function:

Foo operator-(int, const Foo&) { ... }
Pete Becker
  • 74,985
  • 8
  • 76
  • 165
  • When I include: void operator-(int x, const Foo& other); this function I get the error error: 'void Foo::operator-(int, const Foo&)' must take either zero or one argument – Dylan May 15 '17 at 17:38
  • 1
    @Dylan Based on the error message in your edit you appear to have made it a member of `Foo`. It shouldn't be. As stated in the answer above it needs to be a *free* function. – G.M. May 15 '17 at 17:48
  • @G.M. this isn't possible because minute and hour are private. `Foo operator-(int x, const Foo& other)') { int y; y = x - other.min; y -= (60 * other.uur); return y; }` – Dylan May 15 '17 at 18:19
  • 1
    So... it needs to be a [friend](http://en.cppreference.com/w/cpp/language/friend) of class `Foo`. – G.M. May 15 '17 at 18:20
  • oke thank you it worked now – Dylan May 15 '17 at 18:27