for (auto it = betegek.begin(); it != betegek.end();) {
auto& [bolygo, telepesek] = *it;
for (auto it = telepesek.begin(); it != telepesek.end();) {
if (*it == t) {
it = telepesek.erase(it);
} else {
++it;
}
}
if (telepesek.empty()) {
it = betegek.erase(it);
} else {
++it;
}
}
return *this;
Assistant
Rendben, örömmel segítek kijavítani a kódot. A problémák a const módosítók hiányából adódnak a operátoroknál, illetve a < operátor implementációjából.
1. operator+ probléma
Az operator+ a Telepes osztályban nem fogad const objektumokat. Meg kell változtatnod az operátor deklarációját és definícióját, hogy fogadjon const objektumokat:
A többi metódushoz nem kell const? Vagy segíts kérlek, nagyon röviden mondd el, hogy mi alapján döntsem el, hogy kell-e const a { jel elé?
Assistant
A const kulcsszót, amikor egy tagfüggvény végére tesszük, azt jelezzük a fordítónak és a használóknak, hogy a függvény nem módosítja az objektum állapotát. Így a függvények biztonsággal hívhatók const objektumokon is. Általánosan, a következő szempontok alapján dönthetsz:
Getter függvények: Ezek a függvények általában nem módosítják az objektum állapotát, így ezekhez mindenképpen érdemes const kulcsszót hozzáadni. Például a getNev(), getSzulBolygo() stb. metódusok.
Olyan metódusok, amelyek nem változtatják a belső állapotot: Ha egy metódus logikailag nem módosítja az objektum bármely tagváltozóját, -ká tehető. Például a fent említett is ilyen lehet.
User
Köszönöm a segítséget, minden javítás helyes volt, mostmár maximum pontszámolt kaptam rá!
Assistant
Szívesen! Örülök, hogy segíthettem, és gratulálok a maximum pontszámhoz! Ha bármilyen más kérdésed van a jövőben, bátran fordulj hozzám. Sok sikert a továbbiakban is! 😊
teszt15.compile:
"/var/www/html/F/ProgII-hazi/7/1/teszt15.cpp: In function void const_test(const Korhaz&, const Korhaz&):
/var/www/html/F/ProgII-hazi/7/1/teszt15.cpp:12:20: error: no match for operator+ (operand types are const Korhaz and const Korhaz)
12 | Korhaz k3 = k1 + k2;
| ~~ ^ ~~
| | |
| | const Korhaz
| const Korhaz
In file included from /var/www/html/F/ProgII-hazi/7/1/teszt15.cpp:8:
/var/www/html/Hallg/ProgII-hazi/7/h360342/1/feladat.cpp:183:10: note: candidate: Korhaz Korhaz::operator+(const Korhaz&) (near match)
183 | Korhaz operator+(const Korhaz& k2) {
| ^~~~~~~~
/var/www/html/Hallg/ProgII-hazi/7/h360342/1/feladat.cpp:183:10: note: passing const Korhaz* as this argument discards qualifiers
In file included from /usr/include/c++/12/string:47,
from /usr/include/c++/12/bits/locale_classes.h:40,
from /usr/include/c++/12/bits/ios_base.h:41,
from /usr/include/c++/12/ios:42,
from /usr/include/c++/12/ostream:38,
from /usr/include/c++/12/iostream:39,
from /var/www/html/F/ProgII-hazi/7/1/teszt15.cpp:2:
/usr/include/c++/12/bits/stl_iterator.h:630:5: note: candidate: template constexpr std::reverse_iterator<_Iterator> std::operator+(typename reverse_iterator<_Iterator>::difference_type, const reverse_iterator<_Iterator>&)
630 | operator+(typename reverse_iterator<_Iterator>::difference_type __n,
| ^~~~~~~~
/usr/include/c++/12/bits/stl_iterator.h:630:5: note: template argument deduction/substitution failed:
/var/www/html/F/ProgII-hazi/7/1/teszt15.cpp:12:22: note: const Korhaz is not derived from const std::reverse_iterator<_Iterator>
12 | Korhaz k3 = k1 + k2;
| ^~
/usr/include/c++/12/bits/stl_iterator.h:1786:5: note: candidate: template constexpr std::move_iterator<_IteratorL> std::operator+(typename move_iterator<_IteratorL>::difference_type, const move_iterator<_IteratorL>&)
1786 | operator+(typename move_iterator<_Iterator>::difference_type __n,
| ^~~~~~~~
/usr/include/c++/12/bits/stl_iterator.h:1786:5: note: template argument deduction/substitution failed:
/var/www/html/F/ProgII-hazi/7/1/teszt15.cpp:12:22: note: const Korhaz is not derived from const std::move_iterator<_IteratorL>
12 | Korhaz k3 = k1 + k2;
| ^~
In file included from /usr/include/c++/12/string:53:
/usr/include/c++/12/bits/basic_string.h:3432:5: note: candidate: template<class _CharT, class _Traits, class _Alloc> std::__cxx11::basic_string<_CharT, _Traits, _Allocator> std::operator+(const __cxx11::basic_string<_CharT, _Traits, _Allocator>&, const __cxx11::basic_string<_CharT, _Traits, _Allocator>&)
3432 | operator+(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
| ^~~~~~~~
/usr/include/c++/12/bits/basic_string.h:3432:5: note: template argument deduction/substitution failed:
/var/www/html/F/ProgII-hazi/7/1/teszt15.cpp:12:22: note: const Korhaz is not derived from const std::__cxx11::basic_string<_CharT, _Traits, _Allocator>
12 | Korhaz k3 = k1 + k2;
| ^~
In file included from /usr/include/c++/12/string:54:
/usr/include/c++/12/bits/basic_string.tcc:606:5: note: candidate: template<class _CharT, class _Traits, class _Alloc> std::__cxx11::basic_string<_CharT, _Traits, _Allocator> std::operator+(const _CharT*, const __cxx11::basic_string<_CharT, _Traits, _Allocator>&)
606 | operator+(const _CharT* __lhs,
| ^~~~~~~~
/usr/include/c++/12/bits/basic_string.tcc:606:5: note: template argument deduction/substitution failed:
/var/www/html/F/ProgII-hazi/7/1/teszt15.cpp:12:22: note: mismatched types const _CharT* and Korhaz
12 | Korhaz k3 = k1 + k2;
| ^~
/usr/include/c++/12/bits/basic_string.tcc:627:5: note: candidate: template<class _CharT, class _Traits, class _Alloc> std::__cxx11::basic_string<_CharT, _Traits, _Allocator> std::operator+(_CharT, const __cxx11::basic_string<_CharT, _Traits, _Allocator>&)
627 | operator+(_CharT __lhs, const basic_string<_CharT, _Traits, _Alloc>& __rhs)
| ^~~~~~~~
/usr/include/c++/12/bits/basic_string.tcc:627:5: note: template argument deduction/substitution failed:
/var/www/html/F/ProgII-hazi/7/1/teszt15.cpp:12:22: note: const Korhaz is not derived from const std::__cxx11::basic_string<_CharT, _Traits, _Allocator>
12 | Korhaz k3 = k1 + k2;
| ^~
/usr/include/c++/12/bits/basic_string.h:3472:5: note: candidate: template<class _CharT, class _Traits, class _Alloc> std::__cxx11::basic_string<_CharT, _Traits, _Allocator> std::operator+(const __cxx11::basic_string<_CharT, _Traits, _Allocator>&, const _CharT*)
3472 | operator+(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
| ^~~~~~~~
/usr/include/c++/12/bits/basic_string.h:3472:5: note: template argument deduction/substitution failed:
/var/www/html/F/ProgII-hazi/7/1/teszt15.cpp:12:22: note: const Korhaz is not derived from const std::__cxx11::basic_string<_CharT, _Traits, _Allocator>
12 | Korhaz k3 = k1 + k2;
| ^~
/usr/include/c++/12/bits/basic_string.h:3489:5: note: candidate: template<class _CharT, class _Traits, class _Alloc> std::__cxx11::basic_string<_CharT, _Traits, _Allocator> std::operator+(const __cxx11::basic_string<_CharT, _Traits, _Allocator>&, _CharT)
3489 | operator+(const basic_string<_CharT, _Traits, _Alloc>& __lhs, _CharT __rhs)
| ^~~~~~~~
/usr/include/c++/12/bits/basic_string.h:3489:5: note: template argument deduction/substitution failed:
/var/www/html/F/ProgII-hazi/7/1/teszt15.cpp:12:22: note: const Korhaz is not derived from const std::__cxx11::basic_string<_CharT, _Traits, _Allocator>
12 | Korhaz k3 = k1 + k2;
| ^~
/usr/include/c++/12/bits/basic_string.h:3502:5: note: candidate: template<class _CharT, class _Traits, class _Alloc> std::__cxx11::basic_string<_CharT, _Traits, _Allocator> std::operator+(__cxx11::basic_string<_CharT, _Traits, _Allocator>&&, const __cxx11::basic_string<_CharT, _Traits, _Allocator>&)
3502 | operator+(basic_string<_CharT, _Traits, _Alloc>&& __lhs,
| ^~~~~~~~
/usr/include/c++/12/bits/basic_string.h:3502:5: note: template argument deduction/substitution failed:
/var/www/html/F/ProgII-hazi/7/1/teszt15.cpp:12:22: note: types std::__cxx11::basic_string<_CharT, _Traits, _Allocator> and const Korhaz have incompatible cv-qualifiers
12 | Korhaz k3 = k1 + k2;
| ^~
/usr/include/c++/12/bits/basic_string.h:3509:5: note: candidate: template<class _CharT, class _Traits, class _Alloc> std::__cxx11::basic_string<_CharT, _Traits, _Allocator> std::operator+(const __cxx11::basic_string<_CharT, _Traits, _Allocator>&, __cxx11::basic_string<_CharT, _Traits, _Allocator>&&)
3509 | operator+(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
| ^~~~~~~~
/usr/include/c++/12/bits/basic_string.h:3509:5: note: template argument deduction/substitution failed:
/var/www/html/F/ProgII-hazi/7/1/teszt15.cpp:12:22: note: const Korhaz is not derived from const std::__cxx11::basic_string<_CharT, _Traits, _Allocator>
12 | Korhaz k3 = k1 + k2;
| ^~
/usr/include/c++/12/bits/basic_string.h:3516:5: note: candidate: template<class _CharT, class _Traits, class _Alloc> std::__cxx11::basic_string<_CharT, _Traits, _Allocator> std::operator+(__cxx11::basic_string<_CharT, _Traits, _Allocator>&&, __cxx11::basic_string<_CharT, _Traits, _Allocator>&&)
3516 | operator+(basic_string<_CharT, _Traits, _Alloc>&& __lhs,
| ^~~~~~~~
/usr/include/c++/12/bits/basic_string.h:3516:5: note: template argument deduction/substitution failed:
/var/www/html/F/ProgII-hazi/7/1/teszt15.cpp:12:22: note: types std::__cxx11::basic_string<_CharT, _Traits, _Allocator> and const Korhaz have incompatible cv-qualifiers
12 | Korhaz k3 = k1 + k2;
| ^~
/usr/include/c++/12/bits/basic_string.h:3539:5: note: candidate: template<class _CharT, class _Traits, class _Alloc> std::__cxx11::basic_string<_CharT, _Traits, _Allocator> std::operator+(const _CharT*, __cxx11::basic_string<_CharT, _Traits, _Allocator>&&)
3539 | operator+(const _CharT* __lhs,
| ^~~~~~~~
/usr/include/c++/12/bits/basic_string.h:3539:5: note: template argument deduction/substitution failed:
/var/www/html/F/ProgII-hazi/7/1/teszt15.cpp:12:22: note: mismatched types const _CharT* and Korhaz
12 | Korhaz k3 = k1 + k2;
| ^~
/usr/include/c++/12/bits/basic_string.h:3546:5: note: candidate: template<class _CharT, class _Traits, class _Alloc> std::__cxx11::basic_string<_CharT, _Traits, _Allocator> std::operator+(_CharT, __cxx11::basic_string<_CharT, _Traits, _Allocator>&&)
3546 | operator+(_CharT __lhs,
| ^~~~~~~~
/usr/include/c++/12/bits/basic_string.h:3546:5: note: template argument deduction/substitution failed:
/var/www/html/F/ProgII-hazi/7/1/teszt15.cpp:12:22: note: types std::__cxx11::basic_string<_CharT, _Traits, _Allocator> and const Korhaz have incompatible cv-qualifiers
12 | Korhaz k3 = k1 + k2;
| ^~
/usr/include/c++/12/bits/basic_string.h:3553:5: note: candidate: template<class _CharT, class _Traits, class _Alloc> std::__cxx11::basic_string<_CharT, _Traits, _Allocator> std::operator+(__cxx11::basic_string<_CharT, _Traits, _Allocator>&&, const _CharT*)
3553 | operator+(basic_string<_CharT, _Traits, _Alloc>&& __lhs,
| ^~~~~~~~
/usr/include/c++/12/bits/basic_string.h:3553:5: note: template argument deduction/substitution failed:
/var/www/html/F/ProgII-hazi/7/1/teszt15.cpp:12:22: note: types std::__cxx11::basic_string<_CharT, _Traits, _Allocator> and const Korhaz have incompatible cv-qualifiers
12 | Korhaz k3 = k1 + k2;
| ^~
/usr/include/c++/12/bits/basic_string.h:3560:5: note: candidate: template<class _CharT, class _Traits, class _Alloc> std::__cxx11::basic_string<_CharT, _Traits, _Allocator> std::operator+(__cxx11::basic_string<_CharT, _Traits, _Allocator>&&, _CharT)
3560 | operator+(basic_string<_CharT, _Traits, _Alloc>&& __lhs,
| ^~~~~~~~
/usr/include/c++/12/bits/basic_string.h:3560:5: note: template argument deduction/substitution failed:
/var/www/html/F/ProgII-hazi/7/1/teszt15.cpp:12:22: note: types std::__cxx11::basic_string<_CharT, _Traits, _Allocator> and const Korhaz have incompatible cv-qualifiers
12 | Korhaz k3 = k1 + k2;
| ^~"
teszt16.compile:
"/var/www/html/F/ProgII-hazi/7/1/teszt16.cpp: In function void const_test(const Korhaz&, const Korhaz&):
/var/www/html/F/ProgII-hazi/7/1/teszt16.cpp:12:17: error: no match for operator< (operand types are const Korhaz and const Korhaz)
12 | cout << (k1 < k2) << endl; // Expected: true
| ~~ ^ ~~
| | |
| | const Korhaz
| const Korhaz
In file included from /var/www/html/F/ProgII-hazi/7/1/teszt16.cpp:8:
/var/www/html/Hallg/ProgII-hazi/7/h360342/1/feladat.cpp:126:8: note: candidate: bool Korhaz::operator<(Korhaz&) (near match)
126 | bool operator < (Korhaz& k) {
| ^~~~~~~~
/var/www/html/Hallg/ProgII-hazi/7/h360342/1/feladat.cpp:126:8: note: conversion of argument 1 would be ill-formed:
/var/www/html/F/ProgII-hazi/7/1/teszt16.cpp:12:19: error: binding reference of type Korhaz& to const Korhaz discards qualifiers
12 | cout << (k1 < k2) << endl; // Expected: true
| ^~
In file included from /usr/include/c++/12/string:47,
from /usr/include/c++/12/bits/locale_classes.h:40,
from /usr/include/c++/12/bits/ios_base.h:41,
from /usr/include/c++/12/ios:42,
from /usr/include/c++/12/ostream:38,
from /usr/include/c++/12/iostream:39,
from /var/www/html/F/ProgII-hazi/7/1/teszt16.cpp:2:
/usr/include/c++/12/bits/stl_iterator.h:451:5: note: candidate: template constexpr bool std::operator<(const reverse_iterator<_Iterator>&, const reverse_iterator<_Iterator>&)
451 | operator<(const reverse_iterator<_Iterator>& __x,
| ^~~~~~~~
/usr/include/c++/12/bits/stl_iterator.h:451:5: note: template argument deduction/substitution failed:
/var/www/html/F/ProgII-hazi/7/1/teszt16.cpp:12:19: note: const Korhaz is not derived from const std::reverse_iterator<_Iterator>
12 | cout << (k1 < k2) << endl; // Expected: true
| ^~
/usr/include/c++/12/bits/stl_iterator.h:496:5: note: candidate: template<class _IteratorL, class _IteratorR> constexpr bool std::operator<(const reverse_iterator<_Iterator>&, const reverse_iterator<_IteratorR>&)
496 | operator<(const reverse_iterator<_IteratorL>& __x,
| ^~~~~~~~
/usr/include/c++/12/bits/stl_iterator.h:496:5: note: template argument deduction/substitution failed:
/var/www/html/F/ProgII-hazi/7/1/teszt16.cpp:12:19: note: const Korhaz is not derived from const std::reverse_iterator<_Iterator>
12 | cout << (k1 < k2) << endl; // Expected: true
| ^~
/usr/include/c++/12/bits/stl_iterator.h:1683:5: note: candidate: template<class _IteratorL, class _IteratorR> constexpr bool std::operator<(const move_iterator<_IteratorL>&, const move_iterator<_IteratorR>&)
1683 | operator<(const move_iterator<_IteratorL>& __x,
| ^~~~~~~~
/usr/include/c++/12/bits/stl_iterator.h:1683:5: note: template argument deduction/substitution failed:
/var/www/html/F/ProgII-hazi/7/1/teszt16.cpp:12:19: note: const Korhaz is not derived from const std::move_iterator<_IteratorL>
12 | cout << (k1 < k2) << endl; // Expected: true
| ^~
/usr/include/c++/12/bits/stl_iterator.h:1748:5: note: candidate: template constexpr bool std::operator<(const move_iterator<_IteratorL>&, const move_iterator<_IteratorL>&)
1748 | operator<(const move_iterator<_Iterator>& __x,
| ^~~~~~~~
/usr/include/c++/12/bits/stl_iterator.h:1748:5: note: template argument deduction/substitution failed:
/var/www/html/F/ProgII-hazi/7/1/teszt16.cpp:12:19: note: const Korhaz is not derived from const std::move_iterator<_IteratorL>
12 | cout << (k1 < k2) << endl; // Expected: true
| ^~
In file included from /usr/include/c++/12/bits/stl_algobase.h:64,
from /usr/include/c++/12/string:50:
/usr/include/c++/12/bits/stl_pair.h:663:5: note: candidate: template<class _T1, class _T2> constexpr bool std::operator<(const pair<_T1, _T2>&, const pair<_T1, _T2>&)
663 | operator<(const pair<_T1, _T2>& __x, const pair<_T1, _T2>& __y)
| ^~~~~~~~
/usr/include/c++/12/bits/stl_pair.h:663:5: note: template argument deduction/substitution failed:
/var/www/html/F/ProgII-hazi/7/1/teszt16.cpp:12:19: note: const Korhaz is not derived from const std::pair<_T1, _T2>
12 | cout << (k1 < k2) << endl; // Expected: true
| ^~
In file included from /usr/include/c++/12/bits/basic_string.h:47,
from /usr/include/c++/12/string:53:
/usr/include/c++/12/string_view:594:5: note: candidate: template<class _CharT, class _Traits> constexpr bool std::operator<(basic_string_view<_CharT, _Traits>, basic_string_view<_CharT, _Traits>)
594 | operator< (basic_string_view<_CharT, _Traits> __x,
| ^~~~~~~~
/usr/include/c++/12/string_view:594:5: note: template argument deduction/substitution failed:
/var/www/html/F/ProgII-hazi/7/1/teszt16.cpp:12:19: note: Korhaz is not derived from std::basic_string_view<_CharT, _Traits>
12 | cout << (k1 < k2) << endl; // Expected: true
| ^~
/usr/include/c++/12/string_view:600:5: note: candidate: template<class _CharT, class _Traits> constexpr bool std::operator<(basic_string_view<_CharT, _Traits>, __type_identity_t<basic_string_view<_CharT, _Traits> >)
600 | operator< (basic_string_view<_CharT, _Traits> __x,
| ^~~~~~~~
/usr/include/c++/12/string_view:600:5: note: template argument deduction/substitution failed:
/var/www/html/F/ProgII-hazi/7/1/teszt16.cpp:12:19: note: Korhaz is not derived from std::basic_string_view<_CharT, _Traits>
12 | cout << (k1 < k2) << endl; // Expected: true
| ^~
/usr/include/c++/12/string_view:607:5: note: candidate: template<class _CharT, class _Traits> constexpr bool std::operator<(__type_identity_t<basic_string_view<_CharT, _Traits> >, basic_string_view<_CharT, _Traits>)
607 | operator< (__type_identity_t<basic_string_view<_CharT, _Traits>> __x,
| ^~~~~~~~
/usr/include/c++/12/string_view:607:5: note: template argument deduction/substitution failed:
/var/www/html/F/ProgII-hazi/7/1/teszt16.cpp:12:19: note: Korhaz is not derived from std::basic_string_view<_CharT, _Traits>
12 | cout << (k1 < k2) << endl; // Expected: true
| ^~
/usr/include/c++/12/bits/basic_string.h:3691:5: note: candidate: template<class _CharT, class _Traits, class _Alloc> bool std::operator<(const __cxx11::basic_string<_CharT, _Traits, _Allocator>&, const __cxx11::basic_string<_CharT, _Traits, _Allocator>&)
3691 | operator<(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
| ^~~~~~~~
/usr/include/c++/12/bits/basic_string.h:3691:5: note: template argument deduction/substitution failed:
/var/www/html/F/ProgII-hazi/7/1/teszt16.cpp:12:19: note: const Korhaz is not derived from const std::__cxx11::basic_string<_CharT, _Traits, _Allocator>
12 | cout << (k1 < k2) << endl; // Expected: true
| ^~
/usr/include/c++/12/bits/basic_string.h:3704:5: note: candidate: template<class _CharT, class _Traits, class _Alloc> bool std::operator<(const __cxx11::basic_string<_CharT, _Traits, _Allocator>&, const _CharT*)
3704 | operator<(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
| ^~~~~~~~
/usr/include/c++/12/bits/basic_string.h:3704:5: note: template argument deduction/substitution failed:
/var/www/html/F/ProgII-hazi/7/1/teszt16.cpp:12:19: note: const Korhaz is not derived from const std::__cxx11::basic_string<_CharT, _Traits, _Allocator>
12 | cout << (k1 < k2) << endl; // Expected: true
| ^~
/usr/include/c++/12/bits/basic_string.h:3716:5: note: candidate: template<class _CharT, class _Traits, class _Alloc> bool std::operator<(const _CharT*, const __cxx11::basic_string<_CharT, _Traits, _Allocator>&)
3716 | operator<(const _CharT* __lhs,
| ^~~~~~~~
/usr/include/c++/12/bits/basic_string.h:3716:5: note: template argument deduction/substitution failed:
/var/www/html/F/ProgII-hazi/7/1/teszt16.cpp:12:19: note: mismatched types const _CharT* and Korhaz
12 | cout << (k1 < k2) << endl; // Expected: true
| ^~
In file included from /usr/include/c++/12/vector:64,
from /var/www/html/Hallg/ProgII-hazi/7/h360342/1/feladat.cpp:2:
/usr/include/c++/12/bits/stl_vector.h:2074:5: note: candidate: template<class _Tp, class _Alloc> bool std::operator<(const vector<_Tp, _Alloc>&, const vector<_Tp, _Alloc>&)
2074 | operator<(const vector<_Tp, _Alloc>& __x, const vector<_Tp, _Alloc>& __y)
| ^~~~~~~~
/usr/include/c++/12/bits/stl_vector.h:2074:5: note: template argument deduction/substitution failed:
/var/www/html/F/ProgII-hazi/7/1/teszt16.cpp:12:19: note: const Korhaz is not derived from const std::vector<_Tp, _Alloc>
12 | cout << (k1 < k2) << endl; // Expected: true
| ^~
In file included from /usr/include/c++/12/bits/stl_map.h:63,
from /usr/include/c++/12/map:61,
from /var/www/html/Hallg/ProgII-hazi/7/h360342/1/feladat.cpp:3:
/usr/include/c++/12/tuple:1538:5: note: candidate: template<class ... _TElements, class ... _UElements> constexpr bool std::operator<(const tuple<_Args1 ...>&, const tuple<_Args2 ...>&)
1538 | operator<(const tuple<_TElements...>& __t,
| ^~~~~~~~
/usr/include/c++/12/tuple:1538:5: note: template argument deduction/substitution failed:
/var/www/html/F/ProgII-hazi/7/1/teszt16.cpp:12:19: note: const Korhaz is not derived from const std::tuple<_Args1 ...>
12 | cout << (k1 < k2) << endl; // Expected: true
| ^~
/usr/include/c++/12/bits/stl_map.h:1549:5: note: candidate: template<class _Key, class _Tp, class _Compare, class _Alloc> bool std::operator<(const map<_Key, _Tp, _Compare, _Allocator>&, const map<_Key, _Tp, _Compare, _Allocator>&)
1549 | operator<(const map<_Key, _Tp, _Compare, _Alloc>& __x,
| ^~~~~~~~
/usr/include/c++/12/bits/stl_map.h:1549:5: note: template argument deduction/substitution failed:
/var/www/html/F/ProgII-hazi/7/1/teszt16.cpp:12:19: note: const Korhaz is not derived from const std::map<_Key, _Tp, _Compare, _Allocator>
12 | cout << (k1 < k2) << endl; // Expected: true
| ^~
In file included from /usr/include/c++/12/map:62:
/usr/include/c++/12/bits/stl_multimap.h:1170:5: note: candidate: template<class _Key, class _Tp, class _Compare, class _Alloc> bool std::operator<(const multimap<_Key, _Tp, _Compare, _Allocator>&, const multimap<_Key, _Tp, _Compare, _Allocator>&)
1170 | operator<(const multimap<_Key, _Tp, _Compare, _Alloc>& __x,
| ^~~~~~~~
/usr/include/c++/12/bits/stl_multimap.h:1170:5: note: template argument deduction/substitution failed:
/var/www/html/F/ProgII-hazi/7/1/teszt16.cpp:12:19: note: const Korhaz is not derived from const std::multimap<_Key, _Tp, _Compare, _Allocator>
12 | cout << (k1 < k2) << endl; // Expected: true
| ^~
In file included from /usr/include/c++/12/bits/ios_base.h:46:
/usr/include/c++/12/system_error:269:3: note: candidate: bool std::operator<(const error_code&, const error_code&)
269 | operator<(const error_code& __lhs, const error_code& __rhs) noexcept
| ^~~~~~~~
/usr/include/c++/12/system_error:269:31: note: no known conversion for argument 1 from const Korhaz to const std::error_code&
269 | operator<(const error_code& __lhs, const error_code& __rhs) noexcept
| ~~~~~~~~~~~~~~~~~~^~~~~
/usr/include/c++/12/system_error:398:3: note: candidate: bool std::operator<(const error_condition&, const error_condition&)
398 | operator<(const error_condition& __lhs,
| ^~~~~~~~
/usr/include/c++/12/system_error:398:36: note: no known conversion for argument 1 from const Korhaz to const std::error_condition&
398 | operator<(const error_condition& __lhs,
| ~~~~~~~~~~~~~~~~~~~~~~~^~~~~
/var/www/html/F/ProgII-hazi/7/1/teszt16.cpp:13:17: error: no match for operator< (operand types are const Korhaz and const Korhaz)
13 | cout << (k2 < k1) << endl; // Expected: false
| ~~ ^ ~~
| | |
| | const Korhaz
| const Korhaz
/var/www/html/Hallg/ProgII-hazi/7/h360342/1/feladat.cpp:126:8: note: candidate: bool Korhaz::operator<(Korhaz&) (near match)
126 | bool operator < (Korhaz& k) {
| ^~~~~~~~
/var/www/html/Hallg/ProgII-hazi/7/h360342/1/feladat.cpp:126:8: note: conversion of argument 1 would be ill-formed:
/var/www/html/F/ProgII-hazi/7/1/teszt16.cpp:13:19: error: binding reference of type Korhaz& to const Korhaz discards qualifiers
13 | cout << (k2 < k1) << endl; // Expected: false
| ^~
/usr/include/c++/12/bits/stl_iterator.h:451:5: note: candidate: template constexpr bool std::operator<(const reverse_iterator<_Iterator>&, const reverse_iterator<_Iterator>&)
451 | operator<(const reverse_iterator<_Iterator>& __x,
| ^~~~~~~~
/usr/include/c++/12/bits/stl_iterator.h:451:5: note: template argument deduction/substitution failed:
/var/www/html/F/ProgII-hazi/7/1/teszt16.cpp:13:19: note: const Korhaz is not derived from const std::reverse_iterator<_Iterator>
13 | cout << (k2 < k1) << endl; // Expected: false
| ^~
/usr/include/c++/12/bits/stl_iterator.h:496:5: note: candidate: template<class _IteratorL, class _IteratorR> constexpr bool std::operator<(const reverse_iterator<_Iterator>&, const reverse_iterator<_IteratorR>&)
496 | operator<(const reverse_iterator<_IteratorL>& __x,
| ^~~~~~~~
/usr/include/c++/12/bits/stl_iterator.h:496:5: note: template argument deduction/substitution failed:
/var/www/html/F/ProgII-hazi/7/1/teszt16.cpp:13:19: note: const Korhaz is not derived from const std::reverse_iterator<_Iterator>
13 | cout << (k2 < k1) << endl; // Expected: false
| ^~
/usr/include/c++/12/bits/stl_iterator.h:1683:5: note: candidate: template<class _IteratorL, class _IteratorR> constexpr bool std::operator<(const move_iterator<_IteratorL>&, const move_iterator<_IteratorR>&)
1683 | operator<(const move_iterator<_IteratorL>& __x,
| ^~~~~~~~
/usr/include/c++/12/bits/stl_iterator.h:1683:5: note: template argument deduction/substitution failed:
/var/www/html/F/ProgII-hazi/7/1/teszt16.cpp:13:19: note: const Korhaz is not derived from const std::move_iterator<_IteratorL>
13 | cout << (k2 < k1) << endl; // Expected: false
| ^~
/usr/include/c++/12/bits/stl_iterator.h:1748:5: note: candidate: template constexpr bool std::operator<(const move_iterator<_IteratorL>&, const move_iterator<_IteratorL>&)
1748 | operator<(const move_iterator<_Iterator>& __x,
| ^~~~~~~~
/usr/include/c++/12/bits/stl_iterator.h:1748:5: note: template argument deduction/substitution failed:
/var/www/html/F/ProgII-hazi/7/1/teszt16.cpp:13:19: note: const Korhaz is not derived from const std::move_iterator<_IteratorL>
13 | cout << (k2 < k1) << endl; // Expected: false
| ^~
/usr/include/c++/12/bits/stl_pair.h:663:5: note: candidate: template<class _T1, class _T2> constexpr bool std::operator<(const pair<_T1, _T2>&, const pair<_T1, _T2>&)
663 | operator<(const pair<_T1, _T2>& __x, const pair<_T1, _T2>& __y)
| ^~~~~~~~
/usr/include/c++/12/bits/stl_pair.h:663:5: note: template argument deduction/substitution failed:
/var/www/html/F/ProgII-hazi/7/1/teszt16.cpp:13:19: note: const Korhaz is not derived from const std::pair<_T1, _T2>
13 | cout << (k2 < k1) << endl; // Expected: false
| ^~
/usr/include/c++/12/string_view:594:5: note: candidate: template<class _CharT, class _Traits> constexpr bool std::operator<(basic_string_view<_CharT, _Traits>, basic_string_view<_CharT, _Traits>)
594 | operator< (basic_string_view<_CharT, _Traits> __x,
| ^~~~~~~~
/usr/include/c++/12/string_view:594:5: note: template argument deduction/substitution failed:
/var/www/html/F/ProgII-hazi/7/1/teszt16.cpp:13:19: note: Korhaz is not derived from std::basic_string_view<_CharT, _Traits>
13 | cout << (k2 < k1) << endl; // Expected: false
| ^~
/usr/include/c++/12/string_view:600:5: note: candidate: template<class _CharT, class _Traits> constexpr bool std::operator<(basic_string_view<_CharT, _Traits>, __type_identity_t<basic_string_view<_CharT, _Traits> >)
600 | operator< (basic_string_view<_CharT, _Traits> __x,
| ^~~~~~~~
/usr/include/c++/12/string_view:600:5: note: template argument deduction/substitution failed:
/var/www/html/F/ProgII-hazi/7/1/teszt16.cpp:13:19: note: Korhaz is not derived from std::basic_string_view<_CharT, _Traits>
13 | cout << (k2 < k1) << endl; // Expected: false
| ^~
/usr/include/c++/12/string_view:607:5: note: candidate: template<class _CharT, class _Traits> constexpr bool std::operator<(__type_identity_t<basic_string_view<_CharT, _Traits> >, basic_string_view<_CharT, _Traits>)
607 | operator< (__type_identity_t<basic_string_view<_CharT, _Traits>> __x,
| ^~~~~~~~
/usr/include/c++/12/string_view:607:5: note: template argument deduction/substitution failed:
/var/www/html/F/ProgII-hazi/7/1/teszt16.cpp:13:19: note: Korhaz is not derived from std::basic_string_view<_CharT, _Traits>
13 | cout << (k2 < k1) << endl; // Expected: false
| ^~
/usr/include/c++/12/bits/basic_string.h:3691:5: note: candidate: template<class _CharT, class _Traits, class _Alloc> bool std::operator<(const __cxx11::basic_string<_CharT, _Traits, _Allocator>&, const __cxx11::basic_string<_CharT, _Traits, _Allocator>&)
3691 | operator<(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
| ^~~~~~~~
/usr/include/c++/12/bits/basic_string.h:3691:5: note: template argument deduction/substitution failed:
/var/www/html/F/ProgII-hazi/7/1/teszt16.cpp:13:19: note: const Korhaz is not derived from const std::__cxx11::basic_string<_CharT, _Traits, _Allocator>
13 | cout << (k2 < k1) << endl; // Expected: false
| ^~
/usr/include/c++/12/bits/basic_string.h:3704:5: note: candidate: template<class _CharT, class _Traits, class _Alloc> bool std::operator<(const __cxx11::basic_string<_CharT, _Traits, _Allocator>&, const _CharT*)
3704 | operator<(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
| ^~~~~~~~
/usr/include/c++/12/bits/basic_string.h:3704:5: note: template argument deduction/substitution failed:
/var/www/html/F/ProgII-hazi/7/1/teszt16.cpp:13:19: note: const Korhaz is not derived from const std::__cxx11::basic_string<_CharT, _Traits, _Allocator>
13 | cout << (k2 < k1) << endl; // Expected: false
| ^~
/usr/include/c++/12/bits/basic_string.h:3716:5: note: candidate: template<class _CharT, class _Traits, class _Alloc> bool std::operator<(const _CharT*, const __cxx11::basic_string<_CharT, _Traits, _Allocator>&)
3716 | operator<(const _CharT* __lhs,
| ^~~~~~~~
/usr/include/c++/12/bits/basic_string.h:3716:5: note: template argument deduction/substitution failed:
/var/www/html/F/ProgII-hazi/7/1/teszt16.cpp:13:19: note: mismatched types const _CharT* and Korhaz
13 | cout << (k2 < k1) << endl; // Expected: false
| ^~
/usr/include/c++/12/bits/stl_vector.h:2074:5: note: candidate: template<class _Tp, class _Alloc> bool std::operator<(const vector<_Tp, _Alloc>&, const vector<_Tp, _Alloc>&)
2074 | operator<(const vector<_Tp, _Alloc>& __x, const vector<_Tp, _Alloc>& __y)
| ^~~~~~~~
/usr/include/c++/12/bits/stl_vector.h:2074:5: note: template argument deduction/substitution failed:
/var/www/html/F/ProgII-hazi/7/1/teszt16.cpp:13:19: note: const Korhaz is not derived from const std::vector<_Tp, _Alloc>
13 | cout << (k2 < k1) << endl; // Expected: false
| ^~
/usr/include/c++/12/tuple:1538:5: note: candidate: template<class ... _TElements, class ... _UElements> constexpr bool std::operator<(const tuple<_Args1 ...>&, const tuple<_Args2 ...>&)
1538 | operator<(const tuple<_TElements...>& __t,
| ^~~~~~~~
/usr/include/c++/12/tuple:1538:5: note: template argument deduction/substitution failed:
/var/www/html/F/ProgII-hazi/7/1/teszt16.cpp:13:19: note: const Korhaz is not derived from const std::tuple<_Args1 ...>
13 | cout << (k2 < k1) << endl; // Expected: false
| ^~
/usr/include/c++/12/bits/stl_map.h:1549:5: note: candidate: template<class _Key, class _Tp, class _Compare, class _Alloc> bool std::operator<(const map<_Key, _Tp, _Compare, _Allocator>&, const map<_Key, _Tp, _Compare, _Allocator>&)
1549 | operator<(const map<_Key, _Tp, _Compare, _Alloc>& __x,
| ^~~~~~~~
/usr/include/c++/12/bits/stl_map.h:1549:5: note: template argument deduction/substitution failed:
/var/www/html/F/ProgII-hazi/7/1/teszt16.cpp:13:19: note: const Korhaz is not derived from const std::map<_Key, _Tp, _Compare, _Allocator>
13 | cout << (k2 < k1) << endl; // Expected: false
| ^~
/usr/include/c++/12/bits/stl_multimap.h:1170:5: note: candidate: template<class _Key, class _Tp, class _Compare, class _Alloc> bool std::operator<(const multimap<_Key, _Tp, _Compare, _Allocator>&, const multimap<_Key, _Tp, _Compare, _Allocator>&)
1170 | operator<(const multimap<_Key, _Tp, _Compare, _Alloc>& __x,
| ^~~~~~~~
/usr/include/c++/12/bits/stl_multimap.h:1170:5: note: template argument deduction/substitution failed:
/var/www/html/F/ProgII-hazi/7/1/teszt16.cpp:13:19: note: const Korhaz is not derived from const std::multimap<_Key, _Tp, _Compare, _Allocator>
13 | cout << (k2 < k1) << endl; // Expected: false
| ^~
/usr/include/c++/12/system_error:269:3: note: candidate: bool std::operator<(const error_code&, const error_code&)
269 | operator<(const error_code& __lhs, const error_code& __rhs) noexcept
| ^~~~~~~~
/usr/include/c++/12/system_error:269:31: note: no known conversion for argument 1 from const Korhaz to const std::error_code&
269 | operator<(const error_code& __lhs, const error_code& __rhs) noexcept
| ~~~~~~~~~~~~~~~~~~^~~~~
/usr/include/c++/12/system_error:398:3: note: candidate: bool std::operator<(const error_condition&, const error_condition&)
398 | operator<(const error_condition& __lhs,
| ^~~~~~~~
/usr/include/c++/12/system_error:398:36: note: no known conversion for argument 1 from const Korhaz to const std::error_condition&
398 | operator<(const error_condition& __lhs,
| ~~~~~~~~~~~~~~~~~~~~~~~^~~~~"
Kérlek segíts kijavítani a hibákat. Ne a teljes javított kódot küldd el válaszként, hanem mindig csak mutasd meg, hogy mit módosítsak az eredeti kódban, hogy jó legyen.
Illetve a feladat kiértékelő rendszernek ugye a Kórház < operátor sem tetszett, ennek a feladatleírása:
"Valósítsd meg a < operátort két Kórház között. Az a kórház számít kisebbnek, amelyikben a bolygók száma kisebb. Azaz, ha van egy A kórház, amiben egy bolygóról 100 beteget ápolnak, és van egy B kórház, amiben két bolygóról összesen két beteget, akkor az A kórház a kisebb."
Ne feledd a const módosítót a függvény végén is hozzáadni.
2. < operátor a Korhaz osztályban
A < operátor jelenlegi implementációja nem felel meg a leírásnak, és a const problémája is van. Módosítsd a definícióját:
Ez a módosítás biztosítja, hogy a const módosítók helyesen legyenek kezelve, és az operátor a követelményeknek megfelelően működjön, azaz a bolygók számát hasonlítjuk össze.
3. operator+ probléma a Korhaz osztályban
Az operator+ a Korhaz osztályban szintén nem fogad const objektumokat. Módosítsd a deklarációkat és definíciókat:
Ezen módosítások után fordítsd le és futtasd újra a kódot, hogy meggyőződj a hibák megszűnéséről. Ha további problémákba ütközöl, kérlek jelezd!
const
operator==()
Operátorok: Ha operátorokat valamilyen ésszerű értelemben használ az osztály, és ezeket az operátorokat nem változtatják meg az objektum állapotán, akkor ezek is const-osak lehetnek. Például az operator+ esetében, ahol csak lekérdezel adatokat.
Egy const függvény belülről csak más const függvényt hívhat meg ugyanarra az objektumra, és nem módosíthat semmilyen tagváltozót, kivéve ha az mutable.
Hogyan döntsd el?
Ha a függvény csak az objektum állapotával kapcsolatos műveleteket végez (olvas, számít stb.) anélkül, hogy módosítaná az állapotot, akkor legyen const.
Ha a függvény bármilyen változást végez az objektum tagváltozóin (közvetlen vagy közvetett módon), akkor ne használd a const-ot.
Példa:
cpp
string getNev() const {
return nev;
}
Itt a getNev() függvény csak visszaad egy értéket a nev tagból, nem változtatja meg. Emiatt const-ot használunk.
Ha további metódusokról van szó a kódban, és ezek beleillenek ebbe a kategóriába, akkor érdemes a const módosítót használni.