线程化时,C ++错误,std :: invoke:

时间:2018-06-02 07:12:35

标签: c++

好的,这些都是我的错误。 std :: invoke:找不到匹配的重载函数 //和 无法专门化功能模板' unkown-type std :: invoke(" Callable&&,_ Types&& ...)noexcept()'

我真的需要你的帮助。我是C ++的新手,所以如果你给我一些如何做的例子,我想。不仅要解释一下。

这是我的代码:

#include <iostream>
#include <thread>
#include <stdio.h>
#include <Windows.h>
#include <string>




using namespace std;




    void SetColor(int ForgC);
    void Navigation();
    void Switch(int index);
    void UpdateMenu();

    const int IWAL = 1;
    const int ITRI = 0;

    int M_Index = 0;
    int Changes = 0;

    bool Name1 = false;
    bool Name2 = false;

    string bools[2] = { "[OFF]", "[ON]" };

    void Navigation()
    {
        for (;;)
        {
            for (int i = 2; i < 180; i++)
            {
                if (GetAsyncKeyState(i) & 0x8000)
                {
                    switch (i)
                    {
                    case 38:
                        if (M_Index < 2)
                            M_Index++;
                        Changes++;
                        break;
                    case 40:
                        if (M_Index > 0)
                            M_Index--;
                        Changes++;
                        break;
                    case 37:
                        Switch(M_Index);
                        Changes++;
                        break;
                    case 39:
                        Switch(M_Index);
                        Changes++;
                        break;

                    }
                    Sleep(200);
                }
            }
        }
    }

    void Switch(int index)
    {
        if (index == IWAL)
        {
            Name1 = !Name1;
        }
        else if (index == ITRI)
        {
            Name2 = !Name2;
        }

    }

    void UpdateMenu()
    {
        int temp = -1;
        for (;;)
        {
            if (temp != Changes)
            {
                temp = Changes;

                system("cls");

                SetColor(15);
                cout << ">> Krizzo's Menu <<" << endl;
                cout << "___________________" << endl << endl;

                if (M_Index == IWAL)
                {
                    SetColor(10);
                    cout << " Name1\t=\t" << bools[Name1] << endl;
                }
                else
                {
                    SetColor(15);
                    cout << " Name1\t=\t" << bools[Name1] << endl;
                }
                if (M_Index == ITRI)
                {
                    SetColor(10);
                    cout << " Name2\t=\t" << bools[Name2] << endl;
                }
                else
                {
                    SetColor(15);
                    cout << " Name2\t=\t" << bools[Name2] << endl;
                }
            }
        }
    }

    void SetColor(int ForgC)
    {
        WORD wColor;
        //We will need this handle to get the current background attribute
        HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
        CONSOLE_SCREEN_BUFFER_INFO csbi;

        //We use csbi for the wAttributes word.
        if (GetConsoleScreenBufferInfo(hStdOut, &csbi))
        {
            //Mask out all but the background attribute, and add in the foreground color
            wColor = (csbi.wAttributes & 0xF0) + (ForgC & 0x0F);
            SetConsoleTextAttribute(hStdOut, wColor);
        }
        return;
    }

    int main(int argc, char** argv) {
        std::thread t1(Navigation);
        std::thread t2(Switch);
        std::thread t3(UpdateMenu);
        std::thread t4(SetColor);
        t1.join();
        t2.join();
        t3.join();
        t4.join();
        return 0;
    }

1 个答案:

答案 0 :(得分:2)

您的SwitchSetColor函数会使用参数。您必须将此参数传递给thread构造函数。例如:

int addOne(int x)
{
    return x + 1;
}

int main()
{
    std::thread t1(addOne, 5);
    t1.join();
}

处理参考参数时,必须将参数包装在std::ref()调用中:

void addOne(int& x)
{
    x += 1;
}

int main()
{
    int a = 42;
    std::thread t1(addOne, std::ref(a));
    t1.join();
}

最后,当你想在一个单独的线程上运行一个类的成员函数时,你必须传递this指针,因为线程必须知道调用成员函数的实例。

class X
{
    void doSomething()
    {
         std::thread t1(X::expensiveCalculations, this);
         //do something else
         t1.join();
    }

    void expensiveCalculations()
    {

    }
};

int main()
{
    X x;
    x.doSomething();
}