2016-07-30 49 views
1

因爲我想要run使用所有其他元組元素,所以需要這樣做。基本上,我有這些元組的矢量形成一種表格。我無法弄清楚自己如何正確地做到這一點。我可以在元組內分配/移動一個新值到unique_ptr嗎?

編輯:顯然以前,簡化的代碼給出了一個不同的錯誤,所以忽略它。這裏的代碼是我的代碼中的代碼。 (對不起)

class GUI { 
    using win_t = std::tuple<sf::RenderWindow&, Container&, std::unique_ptr<std::thread>, std::condition_variable>; 
    enum { WINDOW, CONT, THREAD, CV } 

    std::vector<win_t> windows;   

    void run(win_t &win); 

    win_t &addWindow(sf::RenderWindow & window, Container & c) { 
     windows.emplace_back(std::forward_as_tuple(window, c, nullptr, std::condition_variable())); 
     win_t &entry = windows.back(); 
     std::get<GUI::THREAD>(entry) = std::make_unique<std::thread>(&GUI::run, this, entry); // error is on this line 
     return entry; 
    } 
} 

和錯誤我得到:

Error C2280 'std::tuple<sf::RenderWindow &,Container &,std::unique_ptr<std::thread,std::default_delete<_Ty>>,std::condition_variable>::tuple(const std::tuple<sf::RenderWindow &,Container &,std::unique_ptr<_Ty,std::default_delete<_Ty>>,std::condition_variable> &)': attempting to reference a deleted function dpomodorivs c:\program files (x86)\microsoft visual studio 14.0\vc\include\tuple 75` 
+0

你嘗試了上面?什麼地方出了錯? – Yakk

+0

請提供[mcve],幷包括您遇到的錯誤。 – Barry

+0

對不起。現在好點了嗎? – snowflake

回答

2

採取在你得到的錯誤仔細一看,替代了類型:

Error C2280 'std::tuple<Ts...>::tuple(const std::tuple<Ts...>&)': attempting to reference a deleted function dpomodorivs c:\program files (x86)\microsoft visual studio 14.0\vc\include\tuple 75` 

你'試圖在您的元組上使用複製構造函數,這是不可複製的(由於unique_ptrcondition_variable)。在該行,就是發生在這裏:

std::make_unique<std::thread>(&GUI::run, this, entry) 

或者更具體地說,在基礎std::thread構造函數調用。 entry不可複製,但thread構造函數在內部複製其所有參數。即使entry可複製,因爲run()將被稱爲thread的副本,而不是您想要的具體entry,所以這不是您想要的。

對於這一點,你需要std::ref()

std::make_unique<std::thread>(&GUI::run, this, std::ref(entry)) 
相關問題