我正在实现头文件“IVideoPlayer.h”,并且创建了一个抽象类“IVideoPlayer”。
class IVideoPlayer
{
public:
// Initialization
virtual bool Load(const char* pFilePath, bool useSubtitles = false) = 0;
virtual bool Start() = 0;
virtual bool Stop() = 0;
//....
};
其功能在文件“VideoPlayer.cpp”中定义
#include "stdafx.h"
#include "IVideoPlayer.h"
#include <dshow.h>
HRESULT hr = CoInitialize(NULL);
IGraphBuilder *pGraph = NULL;
IMediaControl *pControl = NULL;
IMediaEvent *pEvent = NULL;
class VideoPlayer:public IVideoPlayer
{
public:
bool Load(const char* pFilePath, bool useSubtitles = false)
{
EPlaybackStatus var1 = PBS_ERROR;
// Initialize the COM library.
if (FAILED(hr))
{
printf("ERROR - Could not initialize COM library");
return 0;
}
// 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 0;
}
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(L"G:\edit.wmv", NULL);
return 0;
}
bool Start()
{
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.
}
}
return 0;
}
bool Stop()
{
pControl->Release();
pEvent->Release();
pGraph->Release();
CoUninitialize();
return 0;
}
};
并检查头文件,我创建了文件sample.cpp
#include "stdafx.h"
#include "IVideoPlayer.h"
#include <stdio.h>
#include <conio.h>
int main(void)
{
VideoPlayer h;
h.Load("G:\hila.wmv");
getch();
return 0;
}
错误是:
Error 1 error C2065: 'VideoPlayer' : undeclared identifier
Error 2 error C2146: syntax error : missing ';' before identifier 'h'
Error 3 error C2065: 'h' : undeclared identifier
Error 4 error C2065: 'h' : undeclared identifier
Error 5 error C2228: left of '.Load' must have class/struct/union
为什么编译器将其显示为未声明的标识符?
任何帮助都可以接受。预先谢谢你
最佳答案
您绝不会包含任何定义std
命名空间的头文件,因此,(未定义)命名空间的using
会导致错误。您也不包含任何定义VideoPlayer
类的头,主要是因为您决定将类定义放在源文件中,而不是头文件中。
以上解释了两个第一个错误。剩下的错误是由于第二个错误(未定义VideoPlayer
)导致的后续错误。
您需要制作一个放置VideoPlayer
类定义的头文件,非常类似于IVideoPlayer
类的头文件。您将VideoPlayer
成员函数的实现放入源文件中。然后将头文件包含在需要VideoPlayer
类的源文件中。