【发布时间】:2014-09-05 23:07:32
【问题描述】:
我正在使用我自己的 A 类作为地图键的数据值来定义地图。我也在使用地图的 find() 函数。但我得到了错误。
#include<iostream>
#include<map>
using namespace std;
class A{
public:
int x;
A(int a){
x=a;
}
};
int main(){
map<int,A> m;
m[0]=A(3);
m[1]=A(5);
m[2]=A(6);
if(m.find(3) == m.end())
cout<<"none"<<endl;
else
cout<<"done"<<endl;
}
错误
In file included from /usr/include/c++/4.6/map:61:0,
from temp.cpp:2:
/usr/include/c++/4.6/bits/stl_map.h: In member function ‘std::map<_Key, _Tp, _Compare, _Alloc>::mapped_type& std::map<_Key, _Tp, _Compare, _Alloc>::operator[](const key_type&) [with _Key = int, _Tp = A, _Compare = std::less<int>, _Alloc = std::allocator<std::pair<const int, A> >, std::map<_Key, _Tp, _Compare, _Alloc>::mapped_type = A, std::map<_Key, _Tp, _Compare, _Alloc>::key_type = int]’:
temp.cpp:14:5: instantiated from here
/usr/include/c++/4.6/bits/stl_map.h:453:11: error: no matching function for call to ‘A::A()’
/usr/include/c++/4.6/bits/stl_map.h:453:11: note: candidates are:
temp.cpp:7:3: note: A::A(int)
temp.cpp:7:3: note: candidate expects 1 argument, 0 provided
temp.cpp:4:7: note: A::A(const A&)
temp.cpp:4:7: note: candidate expects 1 argument, 0 provided
【问题讨论】:
标签: c++