C++ 中未声明的标识符(错误 C2065)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23539396/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Undeclared identifier (error C2065) in c++
提问by user3615925
I am implementing a header file "IVideoPlayer.h" and I have created an abstract class "IVideoPlayer".
我正在实现一个头文件“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;
//....
};
And the functions of that are defined in file "VideoPlayer.cpp"
其功能在文件“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
并检查我创建的头文件 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
为什么编译器将其显示为未声明的标识符?接受任何帮助。提前致谢
回答by Some programmer dude
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.
您永远不会包含任何定义std
命名空间的头文件,因此using
(未定义的)命名空间会导致错误。您也不包含任何定义VideoPlayer
类的头文件,主要是因为您决定将类定义放在源文件而不是头文件中。
The above accounts for the two first errors. The remaining errors are follow up errors because of the second error (VideoPlayer
not defined).
以上解释了前两个错误。由于第二个错误(VideoPlayer
未定义),其余错误是后续错误。
You need to make a header filewhere 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.
您需要在放置类定义的位置创建一个头文件VideoPlayer
,就像类的头文件一样IVideoPlayer
。您将VideoPlayer
成员函数的实现放在源文件中。然后在需要VideoPlayer
类的源文件中包含头文件。
回答by Vlad from Moscow
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
在您的程序中没有 name std 的定义,因为您没有使用任何包含 namepsace std 定义的标准头文件。至少改变这个指令
#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
此外,您必须将类 'VideoPlayer 的定义放在标题中,并将此标题包含在模块 sample.cpp 中
回答by dennis
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 .
您需要在顶部添加#include。'std' 命名空间在 iostream 库中定义。您还需要在主 cpp 文件中对“视频播放器”类进行前向声明。