thread for member function of object in vector of unique_ptr

I have a vector of unique_ptr of a class Tower. The unique_ptr is to stop object slicing for derived classes. I want to put the function drawTower on another thread. I've tried everything I could find but I keep getting this error:
Error C2661 'std::tuple<void (__thiscall Tower::* )(sf::RenderWindow &,sf::Font,int &),_Ty *,sf::RenderWindow,sf::Font,int>::tuple': no overloaded function takes 5 arguments


1
2
3
4
5
6
7
8
9
10
11
12
13
14
// Tower.h
class Tower {
    void drawTower(sf::RenderWindow& wnd, sf::Font _font, int& gameMoney);
}

// main.cpp
int main() {
    vector<unique_ptr<Tower>> towers;
    thread t1;

    for (int i = 0; i < towers.size(); i++) {
        t1 = thread(&Tower::drawTower, &towers[i], window, f, money);
    }
}
Last edited on
&towers[i] should be towers[i].get().
That's what I thought but it still has the same error.

t1 = thread(&Tower::drawTower, towers[i].get(), window, f, money);
The arguments to the std::thread constructor are by default passed by reference. In order to pass them by reference you need to use std::ref.

 
t1 = thread(&Tower::drawTower, towers[i].get(), ref(window), ref(f), ref(money));
Last edited on
Oh, that makes sense now
Topic archived. No new replies allowed.