2017-08-28 1182 views
1

我有一個叫做TestFunction的函數,我已經簡化了這個問題......但實質上,我收到一個錯誤,說<function-style-cast> cannot convert from 'initializer list' to std::pair<int, int>。這是我的簡化功能:C++無法從初始化程序列表轉換爲std :: pair

#include <iostream> 
#include <map> 

void MyClass::TestFunction(cli::array<int>^ ids){ 

    std::multimap<int, int> mymap; 
    int count = ids->Length; 

    for (int i = 0; i < count; ++i) { 
     //fill in the multimap with the appropriate data key/values 
     mymap.insert(std::make_pair((int)ids[i], (int)i)); 
    } 
} 

正如你可以看到,這是一個非常基本的功能(簡體時),但我得到一個錯誤,當我嘗試將數據插入到多重映射。有誰知道爲什麼?

+0

您的代碼段中沒有任何初始化程序列表。你能發佈完整的錯誤消息嗎? – Rakete1111

+1

C-cli或C++?順便說一句,這裏沒有initializer_list。 – Jarod42

+0

1.您是否檢查過使用這個簡化代碼得到同樣的錯誤? 2.你得到的錯誤是哪一行? –

回答

0

我要麼使用

mymap.insert(std::make_pair((int)ids[i], (int)i)); 

mymap.emplace((int)ids[i], (int)i); 
+0

如果我使用:mymap.insert(std :: make_pair((int)ids [i],(int)i));我得到一個錯誤,說我不能將參數1從'int'轉換爲'int&'。 – andyopayne

0

我建立關閉的@CoryKramer的答案。看起來,如果我創建一個int類型的臨時變量,然後將其傳遞到multimap.insert()函數中...該錯誤已得到解決。這裏的新功能:

#include <iostream> 
#include <map> 

void MyClass::TestFunction(cli::array<int>^ ids){ 

    std::multimap<int, int> mymap; 
    int count = ids->Length; 

    for (int i = 0; i < count; ++i) { 
     //fill in the multimap with the appropriate data key/values 
     int ff = (int)ids[i]; 
     mymap.insert(std::make_pair(ff, (int)i)); 
    } 
} 

出於好奇......有沒有人知道爲什麼這個工作?

相關問題