0

我正在尝试按照http://www.drdobbs.com/cpp/ccli-threading-part-i/184402018上的教程在 Visual c++ 中以 winform 进行线程编程。我打开了一个 win32 控制台项目,并在其中添加了一个空的 cpp 文件,我在其中放置了如下代码:

    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”:委托构造函数的参数无效;委托目标需要是指向成员函数的指针
4

1 回答 1

1

您正在混合 C++ 和 C++/CLI,这是另一回事。代替

public class ThreadX

public ref class ThreadX
于 2013-07-11T12:12:16.957 回答