2013-07-11 39 views
0

我正在試着按照http://www.drdobbs.com/cpp/ccli-threading-part-i/184402018的教程來在Visual C++中使用winform進行線程編程。我開了一個Win32控制檯項目並添加一個空的CPP文件到這裏面,我放置代碼如下:在Visual C++/CLI中給出錯誤的線程程序

using namespace System; 
    using namespace System::Threading; 

    public class ThreadX{ 

     int loopStart; 
     int loopEnd; 
     int dispFrequency; 

     public: 


     ThreadX(int startValue, int endValue, int frequency) 
     { 
      loopStart = startValue; 
      loopEnd = endValue; 
      dispFrequency = frequency; 
     } 

     void ThreadEntryPoint() 
     { 
      String^ threadName = Thread::CurrentThread->Name; 

      for (int i = loopStart; i <= loopEnd; ++i) 
      { 
       if (i % dispFrequency == 0) 
       { 
        Console::WriteLine("{0} : i = {1,10}", threadName, i); 
       } 
      } 
      Console::WriteLine("{0} thread terminating", threadName); 
     } 
}; 

int main() 
{ 
    ThreadX o1 = gcnew ThreadX(0, 1000000,200000); 
    Thread^ t1 = gcnew Thread(gcnew ThreadStart(o1, &ThreadX::ThreadEntryPoint)); 
    t1->Name = "t1"; 

    ThreadX o2 = gcnew ThreadX(-1000000, 0, 200000); 
    Thread^ t2 = gcnew Thread(gcnew ThreadStart(o2, &ThreadX::ThreadEntryPoint)); 
    t1->Name = "t2"; 

    t1->Start(); 
    t2->Start(); 
    Console::WriteLine("Primary Thread Terminating"); 
} 

但是這給了我的錯誤,如:

  1. 錯誤C2726:「gcnew」僅可用於與 管理型
  2. 錯誤C2440創建一個對象:初始化:不能 從「的ThreadX *」到「的ThreadX」沒有構造轉換可以採取 源類型,或構造函數重載解析模棱兩可
  3. 錯誤C3364:'System :: Threading :: ThreadStart':委託構造函數的參數無效;代表目標需要爲 指向成員函數的指針

回答

1

您在混合使用C++和C++/CLI,這是另一回事。更換

public class ThreadX 

public ref class ThreadX 
+0

這正是在上市代碼說 - 顯然複製和粘貼錯誤! – doctorlove

+0

@doc:請刪除以下語句,以防止混用C++和C++/CLI:'#include using namespace std;' –

+0

@ me?我沒有添加他們@ user2572521沒有 - 一個災難性的複製和粘貼,並提出額外的東西了錯誤:-) – doctorlove