2015-09-30 35 views
-3

我從http://www.cplusplus.com/reference/thread/thread/ 得到了一個最小的例子來實現線程。C++ Visual Studio 13使用線程:最小的例子不工作

可惜這個例子沒有編譯錯誤C2664拋出:

// thread example 
#include <iostream>  // std::cout 
#include <thread>   // std::thread 

void foo() 
{ 
    // do stuff... 
} 

void bar(int x) 
{ 
    // do stuff... 
} 

int main() 
{ 
    std::thread first (foo);  // spawn new thread that calls foo() 
    std::thread second (bar,0); // spawn new thread that calls bar(0) 

    std::cout << "main, foo and bar now execute concurrently...\n"; 

    // synchronize threads: 
    first.join();    // pauses until first finishes 
    second.join();    // pauses until second finishes 

    std::cout << "foo and bar completed.\n"; 

    return 0; 
} 

錯誤C2664: '的std ::螺紋::線程(常量的std ::線程&)': 不能轉換參數1 'void'爲'std :: thread & &'

有人可以解釋什麼是錯誤的示例與Visual Studio?

謝謝

+1

不,與VS2012一起工作 – duDE

+3

它也適用於VS2015,你肯定**這是**正好**你正在編譯的代碼?該錯誤涉及哪一行?爲什麼當程序中沒有什麼叫做「n」時,錯誤就會引用「argument n」? –

+1

懷疑:你在實際的代碼中寫了'foo()'而不是'foo'。 – molbdnilo

回答

0

上面的代碼適用於VS 2013(也適用於VS 2012,VS 2015)。

嘗試創建一個新項目並將代碼複製到那裏。 VS有時候表現很奇怪。同時刪除.sdf文件可能會與「構建 - >清理解決方案」一起幫助。

相關問題