【问题标题】:extern keyword "missing type specifier"extern 关键字“缺少类型说明符”
【发布时间】:2012-01-16 07:46:07
【问题描述】:

我正在使用 Visual C++ Express 创建一个 DLL,并且在声明时 extern ValveInterfaces* VIFaceRequired.h 内,编译器告诉我ValveInterfaces 没有定义。 (我想将VIFace 暴露给任何文件,包括Required.h

这是我的文件结构:

DLLMain.cpp

#include "Required.h" //required header files, such as Windows.h and the SDK  

ValveInterfaces* VIFace;  

//the rest of the file

必填.h

#pragma once
//include Windows.h, and the SDK
#include "ValveInterfaces.h"

extern ValveInterfaces* VIFace; //this line errors

ValveInterfaces.h

#pragma once
#ifndef _VALVEINTERFACES_H_
#define _VALVEINTERFACES_H_
#include "Required.h"

class ValveInterfaces
{
public:
    ValveInterfaces(void);
    ~ValveInterfaces(void);
    static CreateInterfaceFn CaptureFactory(char *pszFactoryModule);
    static void* CaptureInterface(CreateInterfaceFn fn, char * pszInterfaceName);
    //globals
    IBaseClientDLL* gClient;
    IVEngineClient* gEngine;
};
#endif

错误截图: http://i.imgur.com/lZBuB.png

【问题讨论】:

  • 您不应该使用保留名称作为包含守卫。虽然这不是您的特定问题的原因(这是由于循环包含 ValveInterfaces.hRequired.h),但它可能导致 similar problems

标签: c++ c extern


【解决方案1】:

第一个错误:

error C2143: syntax error : missing ';' before '*'

在您第一次尝试使用它时没有定义 ValveInterfaces 类型。

这几乎总是会发生,因为ValveInterfaces 的类型未知。有点难以判断,因为您已经删除了大量的 ValveInterfaces.h,但是,即使它在那里定义,它也可能是 #pragma once_REQUIRED_H 包含警卫的明显错位的奇怪组合(他们会通常在required.h),这让你很伤心。

【讨论】:

  • 嗯,我是这么想的,但为什么不是呢?我在声明为 extern 之前包含了头文件。
  • 我已将整个 ValveInterfaces.h 粘贴到里面。我认为我的问题是 ValveInterfaces.h 包含Required.h,它也包含 ValveInterfaces.h。
  • 你可能是对的。我会完全放弃pragma once 并使用更便携的包含警卫。并摆脱循环引用。
【解决方案2】:

请注意,循环包含,就像 Required.hValveInterfaces.h 一样,通常是代码异味。如果您分解循环引用,就不太可能出现此类问题。

您可以尝试做的就是在ValveInterfaces.h 中尽可能多地进行前向声明,并使其保持独立。看起来 ValveInterfaces 不需要 Requires.h 中的所有内容,所以不要包含它。

#ifndef VALVEINTERFACES_H
#define VALVEINTERFACES_H
// CreateInterfaceFn probably need to be fully defined
// so just pull whatever header has that. Don't include 
// Required.h here, there's no need for it.

class IBaseClientDLL;
class IVEngineClient;
class ValveInterfaces
{
public:
    ValveInterfaces();
    ~ValveInterfaces();
    static CreateInterfaceFn CaptureFactory(char *pszFactoryModule);
    static void* CaptureInterface(CreateInterfaceFn fn, char * pszInterfaceName);
    //globals
    IBaseClientDLL* gClient;
    IVEngineClient* gEngine;
};

#endif

【讨论】:

    【解决方案3】:

    您使用ValveInterface(单数)但声明ValveInterfaces(复数)。

    【讨论】:

    • 对不起,输入错误...应该是 ValveInterfaces,编辑它。 Intellisense 未检测到错误 (?)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-19
    相关资源
    最近更新 更多