我正在尝试创建此类的两个实例,它们最终将使用 Win32 的 mciSendString 功能播放音乐文件。但是为了测试它,因为这是我第一次尝试使用 std::thread,我编写了一个 test(void) 方法,它输出类 ID,我希望它打印一系列 1 和 2,例如 12122111112212121212 ...
我收到以下错误,test(void) 方法确实存在吗?
错误 1 错误 C2064:术语不计算为采用 0 个参数的函数
#include <iostream>
#include <thread>
typedef enum MusicStatus {
MUSIC_PLAYING = 0,
MUSIC_PAUSED,
MUSIC_STOPPED,
MUSIC_IDLE
} MusicStatus, *pMusicStatus;
class MusicPlayer
{
public:
MusicPlayer(void) {
m_bIsPlaying = false;
m_bIsPaused = false;
}
bool isPaused(void) {
return m_bIsPaused;
}
bool isPlaying(void) {
return m_bIsPlaying;
}
MusicStatus getState(void) {
if ( !m_bIsPlaying && !m_bIsPaused && !m_bIsStopped )
return MUSIC_IDLE;
if ( m_bIsPlaying )
return MUSIC_PLAYING;
if ( m_bIsPaused )
return MUSIC_PAUSED;
if ( m_bIsStopped )
return MUSIC_STOPPED;
return MUSIC_STOPPED;
}
void test(void) {
for ( int m = 0; m < 100; m++ ) {
std::cout << this->n;
}
}
int n;
private:
bool m_bIsPlaying, m_bIsPaused, m_bIsStopped;
};
int main(int argc, char* argv[])
{
MusicPlayer A;
MusicPlayer B;
A.n = 1;
B.n = 2;
std::thread t1(A);
std::thread t2(B);
t1.join();
t2.join();
A.test();
B.test();
system("PAUSE");
return 0;
}
更新:我做了一些调整,现在我遇到了参数列表的问题,错误:MusicPlayer::play_sound 函数调用缺少参数列表
#include <iostream>
#pragma comment(lib, "Strmiids.lib")
#include <thread>
#include <dshow.h>
#include "Lib/NSL.h"
typedef enum MusicStatus {
MUSIC_PLAYING = 0,
MUSIC_PAUSED,
MUSIC_STOPPED,
MUSIC_IDLE
} MusicStatus, *pMusicStatus;
class MusicPlayer
{
public:
MusicPlayer() {
m_bIsPlaying = false;
m_bIsPaused = false;
m_bIsStopped = false;
}
bool isPaused() {
return m_bIsPaused;
}
bool isPlaying() {
return m_bIsPlaying;
}
MusicStatus getState() {
if ( !m_bIsPlaying && !m_bIsPaused && !m_bIsStopped )
return MUSIC_IDLE;
if ( m_bIsPlaying )
return MUSIC_PLAYING;
if ( m_bIsPaused )
return MUSIC_PAUSED;
if ( m_bIsStopped )
return MUSIC_STOPPED;
return MUSIC_STOPPED;
}
void playAudio(std::string strFilePath) {
m_strFilePath = strFilePath;
std::thread audioThread(play_sound);
audioThread.join();
}
private:
bool m_bIsPlaying, m_bIsPaused, m_bIsStopped;
std::string m_strFilePath;
void play_sound() {
IGraphBuilder *pGraph = NULL;
IMediaControl *pControl = NULL;
IMediaEvent *pEvent = NULL;
// Initialize the COM library.
HRESULT hr = CoInitialize(NULL);
if (FAILED(hr))
{
printf("ERROR - Could not initialize COM library");
return;
}
// Create the filter graph manager and query for interfaces.
hr = CoCreateInstance(CLSID_FilterGraph, NULL, CLSCTX_INPROC_SERVER,
IID_IGraphBuilder, (void **)&pGraph);
if (FAILED(hr))
{
printf("ERROR - Could not create the Filter Graph Manager.");
return;
}
hr = pGraph->QueryInterface(IID_IMediaControl, (void **)&pControl);
hr = pGraph->QueryInterface(IID_IMediaEvent, (void **)&pEvent);
// Build the graph. IMPORTANT: Change this string to a file on your system.
hr = pGraph->RenderFile(s2ws(m_strFilePath).c_str(), NULL);
if (SUCCEEDED(hr))
{
// Run the graph.
hr = pControl->Run();
if (SUCCEEDED(hr))
{
// Wait for completion.
long evCode;
pEvent->WaitForCompletion(INFINITE, &evCode);
// Note: Do not use INFINITE in a real application, because it
// can block indefinitely.
}
}
pControl->Release();
pEvent->Release();
pGraph->Release();
CoUninitialize();
}
};
int main(void)
{
MusicPlayer A;
A.playAudio("music.mp3");
system("pause");
return 0;
}