我在线程方面遇到了一些问题,因为我对它很陌生。
我得到一个:
no instance of constructor "std::thread::thread" matches the argument list
argument types are(void() )
恰好在
std::thread t1(TestPlay);
void CMusicTCPDlg::OnBnClickedBtplaymusic()
{
std::thread t1(TestPlay);
t1.join();
}
void CMusicTCPDlg::TestPlay()
{
if (CFugue::GetMidiOutPortCount() <= 0)
{
std::cerr << "No MIDI Output Ports found!";
exit(-1);
}
std::cout << "Playing Notes..";
CFugue::PlayMusicStringWithOpts(_T("C D E F G A B"), MIDI_MAPPER, 20);
}
我引用了一些线程页面,大多数都有一个像我这样的简单示例。
Visual Studio 建议我在调用该函数之前使用 &,但它无法使用它。 我必须改为使用 BackgroundWorker 吗?
如果这是重复的,真的很抱歉。谢谢!
最佳答案
TestPlay
是一个成员函数,这意味着它的类型是void (CMusicTCPDlg::)()
。
您需要提供绑定(bind)版本以允许线程调用它:std::bind(&TestPlay, this)
。请注意,您必须确保线程存在的时间不超过对象本身,否则将导致未定义的行为。 (它将在一个不存在的对象上执行一个函数)
关于C++/线程 : No instance of constructor "std::thread::thread" > matches the argument list,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40770913/