任何機構都可以幫我解決這個問題。我有一個類Comm
存儲另一個類Info
的元素在地圖容器:錯誤:無法調用的功能
#include<map>
using namespace std;
class En
{
};
class Info
{
public:
const En * en;
bool read;
bool write;
bool done;
Info(
En * en_,
bool read_,
bool write_,
bool done_
)
:
en(en_),
read(read_),
write(write_),
done(done_)
{}
Info(const Info& info_)
:
en(info_.en),
read(info_.read),
write(info_.write),
done(info_.done)
{}
};
class Comm
{
std::map<const En*,Info> subscriptionList;
public:
void subscribeEn(Info value)
{
//none of the below works
// subscriptionList[value.en] = Info(value);
subscriptionList[value.en] = value;
}
};
int main()
{
// En * en;
// bool read;
// bool write;
// bool Done;
// Comm comm;
// Info Info_(en,read,write,Done);
// comm.subscribeEn(Info_);
// return 1;
}
但我得到的編譯以下錯誤:
In file included from /usr/include/c++/4.7/map:61:0,
from test.cpp:1:
/usr/include/c++/4.7/bits/stl_map.h: In instantiation of ‘std::map<_Key, _Tp, _Compare, _Alloc>::mapped_type& std::map<_Key, _Tp, _Compare, _Alloc>::operator[](const key_type&) [with _Key = const En*; _Tp = Info; _Compare = std::less<const En*>; _Alloc = std::allocator<std::pair<const En* const, Info> >; std::map<_Key, _Tp, _Compare, _Alloc>::mapped_type = Info; std::map<_Key, _Tp, _Compare, _Alloc>::key_type = const En*]’:
test.cpp:47:27: required from here
/usr/include/c++/4.7/bits/stl_map.h:458:11: error: no matching function for call to ‘Info::Info()’
/usr/include/c++/4.7/bits/stl_map.h:458:11: note: candidates are:
test.cpp:28:2: note: Info::Info(const Info&)
test.cpp:28:2: note: candidate expects 1 argument, 0 provided
test.cpp:15:2: note: Info::Info(En*, bool, bool, bool)
test.cpp:15:2: note: candidate expects 4 arguments, 0 provided
如果你讓我知道爲什麼我讓我欣賞這和如何解決它。 謝謝
'map :: operator []'需要在它可以分配給它之前創建一個對象(如果它尚未)。它將如何使用默認的構造函數創建該對象? – chris 2013-03-11 05:37:45
我提供了一些Info類的構造函數,但無法修復它。你能幫我戴上帽子嗎?謝謝 – rahman 2013-03-11 05:43:16
如果你完全反對有一個默認的構造函數,你將不得不使用'insert'(或類似的)和'at'。對它進行下標會在地圖中創建默認值。 – chris 2013-03-11 05:47:03