私は自分のクラスを整数にマップしようとしています:
struct point{
int x;
int y;
bool operator==(const point& b) {
return (x==b.x && y==b.y);
};
bool operator<(const point& b) {
return (x < b.x && y < b.y);
};
};
int main() {
point a, b;
a.x = 0;
a.y = 1;
b.x = 0;
b.y = 1;
map<point, int> myMap;
myMap[a] = 1;
cout << (a==b) << endl;
cout << myMap.count(b) << endl;
}
このコードはコンパイルされず、エラー メッセージを理解できません。に変更するmap<point*, int>
とコンパイルされるようですが、それは私が望むものではありません。私の目的は、 を呼び出したときに 1mayMap.count(b)
が返されるようにすることです。どのような変更を加える必要がありますか? どうもありがとうございました!エラーメッセージ:
-*- mode: compilation; default-directory: "~/Dropbox/Documents/UVa/Others/UVa Online Judge/" -*-
Compilation started at Tue Nov 12 14:04:15
clang++ testset.cpp
In file included from testset.cpp:1:
In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/c++/v1/iostream:38:
In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/c++/v1/ios:216:
In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/c++/v1/__locale:15:
In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/c++/v1/string:434:
In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/c++/v1/algorithm:594:
In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/c++/v1/memory:597:
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/c++/v1/__functional_base:56:21: error:
invalid operands to binary expression ('const point' and 'const point')
{return __x < __y;}
~~~ ^ ~~~
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/c++/v1/map:1032:17: note:
in instantiation of member function 'std::__1::less<point>::operator()'
requested here
if (__tree_.value_comp().key_comp()(__k, __nd->__value_.first))
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/c++/v1/map:1272:36: note:
in instantiation of member function 'std::__1::map<point, int,
std::__1::less<point>, std::__1::allocator<std::__1::pair<const point,
int> > >::__find_equal_key' requested here
__node_base_pointer& __child = __find_equal_key(__parent, __k);
^
testset.cpp:41:12: note: in instantiation of member function
'std::__1::map<point, int, std::__1::less<point>,
std::__1::allocator<std::__1::pair<const point, int> > >::operator[]'
requested here
secondMap[a] = 1;
^
testset.cpp:13:8: note: candidate function not viable: 'this' argument has type
'const point', but method is not marked const
bool operator<(const point& b) {
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/c++/v1/utility:375:1: note:
candidate template ignored: failed template argument deduction
operator< (const pair<_T1,_T2>& __x, const pair<_T1,_T2>& __y)
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/c++/v1/iterator:565:1: note:
candidate template ignored: failed template argument deduction
operator<(const reverse_iterator<_Iter1>& __x, const reverse_iterator<_I...
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/c++/v1/iterator:960:1: note:
candidate template ignored: failed template argument deduction
operator<(const move_iterator<_Iter1>& __x, const move_iterator<_Iter2>& __y)
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/c++/v1/iterator:1269:1: note:
candidate template ignored: failed template argument deduction
operator<(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter2>& __y...
^
1 error generated.
Compilation exited abnormally with code 1 at Tue Nov 12 14:04:16