Przeładowanie operatora < (ile argumentów?)

0
#include <iostream>
using namespace std;
 
struct whatever
{
    int liczba;
    bool operator < (whatever a, whatever b)
    {
        return (a.liczba<b.liczba);
    }
};
 
int main()
{
    whatever a; a.liczba=20; whatever b; b.liczba=25; cout << (a<b);
    return 0;
}

Się nawet nie kompiluje:

prog.cpp:7:44: error: ‘bool whatever::operator<(whatever, whatever)’ must take exactly one argument
     bool operator < (whatever a, whatever b)
                                            ^
prog.cpp: In function ‘int main()’:
prog.cpp:15:65: error: no match for ‘operator<’ (operand types are ‘whatever’ and ‘whatever’)
     whatever a; a.liczba=20; whatever b; b.liczba=25; cout << (a<b);
                                                                 ^
prog.cpp:15:65: note: candidates are:
In file included from /usr/include/c++/4.8/bits/stl_algobase.h:64:0,
                 from /usr/include/c++/4.8/bits/char_traits.h:39,
                 from /usr/include/c++/4.8/ios:40,
                 from /usr/include/c++/4.8/ostream:38,
                 from /usr/include/c++/4.8/iostream:39,
                 from prog.cpp:1:
/usr/include/c++/4.8/bits/stl_pair.h:220:5: note: template<class _T1, class _T2> bool std::operator<(const std::pair<_T1, _T2>&, const std::pair<_T1, _T2>&)
     operator<(const pair<_T1, _T2>& __x, const pair<_T1, _T2>& __y)
     ^
/usr/include/c++/4.8/bits/stl_pair.h:220:5: note:   template argument deduction/substitution failed:
prog.cpp:15:66: note:   ‘whatever’ is not derived from ‘const std::pair<_T1, _T2>’
     whatever a; a.liczba=20; whatever b; b.liczba=25; cout << (a<b);
                                                                  ^
In file included from /usr/include/c++/4.8/bits/stl_algobase.h:67:0,
                 from /usr/include/c++/4.8/bits/char_traits.h:39,
                 from /usr/include/c++/4.8/ios:40,
                 from /usr/include/c++/4.8/ostream:38,
                 from /usr/include/c++/4.8/iostream:39,
                 from prog.cpp:1:
/usr/include/c++/4.8/bits/stl_iterator.h:297:5: note: template<class _Iterator> bool std::operator<(const std::reverse_iterator<_Iterator>&, const std::reverse_iterator<_Iterator>&)
     operator<(const reverse_iterator<_Iterator>& __x,
     ^
/usr/include/c++/4.8/bits/stl_iterator.h:297:5: note:   template argument deduction/substitution failed:
prog.cpp:15:66: note:   ‘whatever’ is not derived from ‘const std::reverse_iterator<_Iterator>’
     whatever a; a.liczba=20; whatever b; b.liczba=25; cout << (a<b);
                                                                  ^
In file included from /usr/include/c++/4.8/bits/stl_algobase.h:67:0,
                 from /usr/include/c++/4.8/bits/char_traits.h:39,
                 from /usr/include/c++/4.8/ios:40,
                 from /usr/include/c++/4.8/ostream:38,
                 from /usr/include/c++/4.8/iostream:39,
                 from prog.cpp:1:
/usr/include/c++/4.8/bits/stl_iterator.h:347:5: note: template<class _IteratorL, class _IteratorR> bool std::operator<(const std::reverse_iterator<_Iterator>&, const std::reverse_iterator<_IteratorR>&)
     operator<(const reverse_iterator<_IteratorL>& __x,
     ^
/usr/include/c++/4.8/bits/stl_iterator.h:347:5: note:   template argument deduction/substitution failed:
prog.cpp:15:66: note:   ‘whatever’ is not derived from ‘const std::reverse_iterator<_Iterator>’
     whatever a; a.liczba=20; whatever b; b.liczba=25; cout << (a<b);
                                                                  ^
In file included from /usr/include/c++/4.8/string:52:0,
                 from /usr/include/c++/4.8/bits/locale_classes.h:40,
                 from /usr/include/c++/4.8/bits/ios_base.h:41,
                 from /usr/include/c++/4.8/ios:42,
                 from /usr/include/c++/4.8/ostream:38,
                 from /usr/include/c++/4.8/iostream:39,
                 from prog.cpp:1:
/usr/include/c++/4.8/bits/basic_string.h:2569:5: note: template<class _CharT, class _Traits, class _Alloc> bool std::operator<(const std::basic_string<_CharT, _Traits, _Alloc>&, const std::basic_string<_CharT, _Traits, _Alloc>&)
     operator<(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
     ^
/usr/include/c++/4.8/bits/basic_string.h:2569:5: note:   template argument deduction/substitution failed:
prog.cpp:15:66: note:   ‘whatever’ is not derived from ‘const std::basic_string<_CharT, _Traits, _Alloc>’
     whatever a; a.liczba=20; whatever b; b.liczba=25; cout << (a<b);
                                                                  ^
In file included from /usr/include/c++/4.8/string:52:0,
                 from /usr/include/c++/4.8/bits/locale_classes.h:40,
                 from /usr/include/c++/4.8/bits/ios_base.h:41,
                 from /usr/include/c++/4.8/ios:42,
                 from /usr/include/c++/4.8/ostream:38,
                 from /usr/include/c++/4.8/iostream:39,
                 from prog.cpp:1:
/usr/include/c++/4.8/bits/basic_string.h:2581:5: note: template<class _CharT, class _Traits, class _Alloc> bool std::operator<(const std::basic_string<_CharT, _Traits, _Alloc>&, const _CharT*)
     operator<(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
     ^
/usr/include/c++/4.8/bits/basic_string.h:2581:5: note:   template argument deduction/substitution failed:
prog.cpp:15:66: note:   ‘whatever’ is not derived from ‘const std::basic_string<_CharT, _Traits, _Alloc>’
     whatever a; a.liczba=20; whatever b; b.liczba=25; cout << (a<b);
                                                                  ^
In file included from /usr/include/c++/4.8/string:52:0,
                 from /usr/include/c++/4.8/bits/locale_classes.h:40,
                 from /usr/include/c++/4.8/bits/ios_base.h:41,
                 from /usr/include/c++/4.8/ios:42,
                 from /usr/include/c++/4.8/ostream:38,
                 from /usr/include/c++/4.8/iostream:39,
                 from prog.cpp:1:
/usr/include/c++/4.8/bits/basic_string.h:2593:5: note: template<class _CharT, class _Traits, class _Alloc> bool std::operator<(const _CharT*, const std::basic_string<_CharT, _Traits, _Alloc>&)
     operator<(const _CharT* __lhs,
     ^
/usr/include/c++/4.8/bits/basic_string.h:2593:5: note:   template argument deduction/substitution failed:
prog.cpp:15:66: note:   mismatched types ‘const _CharT*’ and ‘whatever’
     whatever a; a.liczba=20; whatever b; b.liczba=25; cout << (a<b);
                                                                  ^

http://ideone.com/tInBuJ

WTF??? Co to znaczy: "error: ‘bool whatever::operator<(whatever, whatever)’ must take exactly one argument"???

Jak to: JEDEN ARGUMENT? Jak żesz mam porównywać JEDEN ARGUMENT?!? Co, mam porównywać strukturę samą ze sobą? O ile mi wiadomo, operator < jest operatorem binarnym, nie unarnym, czy tak?

Zatem: Jak mam go przeładować, żeby nie wyskakiwał błąd?

Z góry dzięki.

0

Funkcja operatora przyjmuje dokladnie tyle samo argumentow co sam operator. Co w przypadku operatorow binarnych, daje wynik 2.
Natomiast Twoja funkcja przyjmuje 3 argumenty: this, a i b. Kompilatorowi sie to po prostu nie podoba.

0

Podczas przeciążania operatora dwuargumentowego w klasie, pierwszy argument jest domyślny i nie trzeba (a w zasadzie, jak widzisz, nie można) go uwzględniać, jest nim obiekt klasy, w której przeciążasz operator.

Tak na szybko napisany kod, bez sprawdzania poprawności: https://ideone.com/E3VWJc

#include <iostream>

using namespace std;

class baton
 {
  public:
   string nazwa;
   int cena;
   baton(int,string);
   bool operator < (baton&);
 };

int main(void)
 {
  baton a(3,"Smaczny"),b(1,"Niedobry");
  cout<<"Tanszy baton jest "<<(a<b?a.nazwa:b.nazwa)<<'\n'; //b to inny baton
  return 0;
 }

bool baton::operator <(baton& inny)
 {
  return cena < inny.cena;
 }

baton::baton(int x,string y):cena(x),nazwa(y)
 {}

Mam nadzieję, że się gdzieś nie pomyliłem.

0

To co opisał wyżej @Tacet to jest tylko jedno z rozwiązań (wg mnie najlepsze) ale można to zrobić również tak:
http://ideone.com/gnbuq3
lub tak:
http://ideone.com/hy6RzC

Właściwie to oba te rozwiązania są identyczne ale różnica jednak jest.

0

No cóż, dzięki...

1 użytkowników online, w tym zalogowanych: 0, gości: 1