【问题标题】:Undeclared identifier (error C2065) in c++c++ 中未声明的标识符(错误 C2065)
【发布时间】:2014-05-08 10:39:34
【问题描述】:

我正在实现一个头文件“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 

为什么编译器将其显示为未声明的标识符? 接受任何帮助。提前谢谢你

【问题讨论】:

  • 如果你甚至没有使用 c++ 库中的东西,你为什么要写using namespace std;?通常也不应该这样做!
  • 添加#include "resource.h"

标签: c++ visual-studio-2008


【解决方案1】:

您永远不会包含任何定义 std 命名空间的头文件,因此 using 那个(未定义的)命名空间会导致错误。您也没有包含任何定义 VideoPlayer 类的标头,主要是因为您决定将类定义放在源文件而不是头文件中。

以上解释了前两个错误。由于第二个错误(VideoPlayer 未定义),其余错误为后续错误。


您需要创建一个头文件,在其中放置VideoPlayer 类定义,很像IVideoPlayer 类的头文件。您将VideoPlayer 成员函数的实现放在源文件中。然后在需要VideoPlayer类的源文件中包含头文件。

【讨论】:

  • 感谢您的建议。我删除了命名空间,第一个错误消失了。但是编译器仍然显示铰孔错误。是不是因为头文件名和相关源文件名不一样?
  • @user3615925:不,这是因为您在源文件中定义了VideoPlayer,而不是在头文件中,所以它在其他源文件中不可用。您需要将类定义移动到标题中,并将其包含在 main.cpp 中。
【解决方案2】:

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

#include <stdio.h>

#include <cstdio>

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

【讨论】:

    【解决方案3】:

    您需要在顶部添加#include。 'std' 命名空间在 iostream 库中定义。 此外,您还需要在主 cpp 文件中对“视频播放器”类进行前向声明。

    【讨论】:

    • 这以何种方式回答了 OP 的问题?
    • 你为什么要包含一个你不需要的标题只是为了允许你不需要的 using 指令,它首先不应该存在?
    • @MikeSeymour:也许你是对的。如果我们没有使用“std”命名空间提供的任何功能,那么它就不需要。但最好让开发人员知道导致错误的缺失链接是什么。在这种情况下,头文件包含是缺失的链接,至少我最初是这么想的。 :)
    • 如果你想创建一个对象,你需要的不仅仅是前向声明。 VideoPlayer的定义需要移到header中。
    • @MikeSeymour:是的,没错!!我的错。感谢您纠正我。
    猜你喜欢
    • 2011-02-15
    • 1970-01-01
    • 2011-04-16
    • 2011-04-16
    • 2010-12-24
    • 2023-03-04
    • 2011-03-02
    • 2011-12-22
    • 1970-01-01
    相关资源
    最近更新 更多