Undeclared identifier (error C2065) in c++

守給你的承諾、 提交于 2019-12-25 01:09:42

问题


I am implementing a header file "IVideoPlayer.h" and I have created an abstract class "IVideoPlayer".

class IVideoPlayer
{

public:
// Initialization
virtual bool Load(const char* pFilePath, bool useSubtitles = false) = 0;
    virtual bool Start() = 0;
    virtual bool Stop() = 0;
    //....
};

And the functions of that are defined in file "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;

    }
};

And to check the header file I have created file 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;
}

The errors are:

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 

Why compiler is showing it as undeclared identifer? Any help is accepted. Thanking you in advance


回答1:


You never include any header files which defined the std namespace, so using that (undefined) namespace results in an error. You also don't include any header which defines the VideoPlayer class, mostly because you have decided to put the class definition in a source file instead of a header file.

The above accounts for the two first errors. The remaining errors are follow up errors because of the second error (VideoPlayer not defined).


You need to make a header file where you put the VideoPlayer class definition, much like the header file for the IVideoPlayer class. You put the implementation of the VideoPlayer member functions in the source file. Then include the header file in the source file where you need the VideoPlayer class.




回答2:


There is no definition of name std in your program because you do not use any standard header that contains the definition of namepsace std. At least change this directive

#include <stdio.h>

to

#include <cstdio>

Also you have to place the definition of class 'VideoPlayer in a header and include this header in module sample.cpp




回答3:


You need to add #include at the top. 'std' namespace is defined in iostream library. Also you need to do a forward declaration of the "Video Player" class in your main cpp file .



来源:https://stackoverflow.com/questions/23539396/undeclared-identifier-error-c2065-in-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!