我一直在試圖學習如何多線程,但我無法正確創建線程對象。我有一個名爲task的函數,但是當我添加函數和參數時,它說構造函數不會接受它。順便說一句,即時通訊使用Visual Studio作爲我的IDE。不能使用線程構造函數
這裏是我的主文件:
#include <iostream>
#include <thread>
#include "Task.h"
using namespace std;
int main(int argc, char** argv)
{
Task t;
thread t1(t.task, 1);
t1.join;
return 0;
}
類任務的對象:
#include "Task.h"
#include <iostream>
using namespace std;
Task::Task()
{
}
Task::~Task()
{
}
void Task::task(int x) {
cout << "In Thread " << x << '\n';
}
錯誤:
更新: 所以我把在thread t1(&Task::task, &t, 1);
和整肅t1.join
,但現在我有一個新問題。程序編譯器和運行,但它運行時的權利,則顯示「在線程1」在控制檯上,而另一個窗口彈出,上面寫着:
Debug Error!
abort() has been called
(Press retry to debug the application)
Visual Studio _what_?有很多版本。 –
't1.join'不會編譯,你應該總是嘗試發佈你的真實代碼,而不是當場輸入。 –
我正在使用視覺工作室專業2015 –