2013-03-11 84 views
0

任何機構都可以幫我解決這個問題。我有一個類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 

如果你讓我知道爲什麼我讓我欣賞這和如何解決它。 謝謝

+3

'map :: operator []'需要在它可以分配給它之前創建一個對象(如果它尚未)。它將如何使用默認的構造函數創建該對象? – chris 2013-03-11 05:37:45

+0

我提供了一些Info類的構造函數,但無法修復它。你能幫我戴上帽子嗎?謝謝 – rahman 2013-03-11 05:43:16

+1

如果你完全反對有一個默認的構造函數,你將不得不使用'insert'(或類似的)和'at'。對它進行下標會在地圖中創建默認值。 – chris 2013-03-11 05:47:03

回答

5

我相信,這個問題就在這裏:

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; 
    } 
}; 

我猜,這std::map首先實例與const En*Info一對,然後使分配這副領域。您還沒有爲Info提供無參數構造函數,這就是編譯器抱怨的原因。

您可以通過添加解決這個問題下面您的信息類:

// Default, parameterless constructor 
Info() 
{ 
    // Some default values 
    en = NULL; // or nullptr in C++11 
    read = false; 
    write = false; 
    done = false; 
} 

另一種解決方案是改變的std ::地圖定義,使得它包含一個指向信息,而不是它的實例:

std::map<const En *,Info *> subscriptionList; 
+0

這是正確的答案,不過你說關於類的定義是不正確的,因爲沒有'const'數據成員(而是一個指向const的指針)。 – juanchopanza 2013-03-11 07:15:31

+0

Aww廢話。你是對的,我把'const en *'和'En * const'混淆了:) – Spook 2013-03-11 07:16:50

+0

這是一個容易犯的錯誤!順便說一句,現在所有的OP都需要給他們一個關於如何編寫默認構造函數的例子。 – juanchopanza 2013-03-11 07:18:18

2

done不正確的大寫:

bool Done; 
Info Info_(en,read,write,done); 

通常小寫或駝峯用於變量名。

+0

是的,我會編輯它。但這不是問題的根源 – rahman 2013-03-11 05:46:03

+1

夥計,錯字不是這個問題的主題 – rahman 2013-03-11 06:09:57

+2

它當然是*話題,特別是考慮到它是您的編譯輸出中列出的錯誤之一。現在你已經編輯了這個問題,它是無關緊要的。這是你的錯,不是我的。 – 2013-03-11 06:11:14