visual studio 2008 - c++中未声明的标识符(错误C2065)

visual studio 2008 - Undeclared identifier (error C2065) in c++

本文关键字:标识符 错误 C2065 未声明 2008 studio c++ visual      更新时间:2023-10-16

我正在实现一个头文件"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类的源文件中。

程序中没有std名称的定义,因为您没有使用任何包含命名空间std定义的标准头文件。至少修改这个指令

#include <stdio.h>

#include <cstdio>

还必须将类'VideoPlayer '的定义放在一个头文件中,并将该头文件包含在模块sample.cpp

您需要在顶部添加#include。'std'命名空间在iostream库中定义。你还需要在你的主cpp文件中做一个"Video Player"类的前向声明。