visual-studio 错误 C2504:“BASECLASS”:未定义基类

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3053739/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-22 12:06:59  来源:igfitidea点击:

error C2504: 'BASECLASS' : base class undefined

c++cvisual-studiovisual-studio-2008visual-c++

提问by numerical25

I checked out a post similar to this but the linkage was different the issue was never resolved. The problem with mine is that for some reason the linker is expecting there to be a definition for the base class, but the base class is just a interface. Below is the error in it's entirety

我查看了一个与此类似的帖子,但链接不同,问题从未解决。我的问题是,由于某种原因,链接器期望基类有一个定义,但基类只是一个接口。以下是错误的全部内容

c:\users\numerical25\desktop\intro todirectx\godfiles\gxrendermanager\gxrendermanager\gxrendermanager\gxdx.h(2) : error C2504: 'GXRenderer' : base class undefined

Below is the code that shows how the headers link with one another

下面是显示标题如何相互链接的代码

GXRenderManager.h

GXRenderManager.h

#ifndef GXRM
#define GXRM
#include <windows.h>
#include "GXRenderer.h"
#include "GXDX.h"
#include "GXGL.h"

enum GXDEVICE {
    DIRECTX,
    OPENGL
};

class GXRenderManager {
public:
    static int Ignite(GXDEVICE);

private:
    static GXRenderer *renderDevice;

};

#endif

at the top of GxRenderManager, there is GXRenderer , windows, GXDX, GXGL headers. I am assuming by including them all in this document. they all link to one another as if they were all in the same document. correct me if I am wrong cause that's how a view headers. Moving on...

在 GxRenderManager 的顶部,有 GXRenderer 、 windows、 GXDX、 GXGL 头文件。我假设将它们全部包含在本文档中。它们都相互链接,就好像它们都在同一个文档中一样。如果我错了,请纠正我,因为这就是视图标题的方式。继续...

GXRenderer.h

GXRenderer.h

class GXRenderer {

public:
    virtual void Render() = 0;
    virtual void StartUp() = 0;

};

GXGL.h

GXGL.h

class GXGL: public GXRenderer {

public:
    void Render();
    void StartUp();
};

GXDX.h

GXDX.h

class GXDX: public GXRenderer {
public:
    void Render();
    void StartUp();
};

GXGL.cpp and GXDX.cpp respectively

GXGL.cpp 和 GXDX.cpp 分别

#include "GXGL.h"

void GXGL::Render()
{

}

void GXGL::StartUp()
{

}

//...Next document

#include "GXDX.h"


void GXDX::Render()
{

}

void GXDX::StartUp()
{

}

Not sure whats going on. I think its how I am linking the documents, I am not sure.

不知道发生了什么。我认为它是如何链接文件的,我不确定。

采纳答案by Brian R. Bondy

The problem is You need to have #include "GXRenderer.h"at the top of both: GXGL.h and also GXDX.h.

问题是你需要同时拥有#include "GXRenderer.h":GXGL.h 和 GXDX.h。

The base type must be defined not just declared before defining a derived type.

必须定义基类型,而不仅仅是在定义派生类型之前声明。

By the way, the error is a compiling error not linking error.

顺便说一句,错误是编译错误而不是链接错误。

Edit: About your class type redefinition:

编辑:关于您的类类型重新定义:

at the top of every header file you should have #pragma once.

在您应该拥有的每个头文件的顶部#pragma once

The #pragma oncedirective specifies that the file will be included at most once by the compiler in a build.

#pragma once指令指定编译器在构建中最多包含一次文件。

回答by AnT

You included them all into GXRenderManager.h, meaning that GXRenderManager.his OK.

您将它们全部包含在 中GXRenderManager.h,这意味着GXRenderManager.h可以。

But you forgot to include them all into GXGL.cppand GXDX.cpp. In these .cpp files GXRendererclass is completely unknown.

但是您忘记将它们全部包含在GXGL.cpp和 中GXDX.cpp。在这些 .cpp 文件中,GXRenderer类是完全未知的。

There are at least two "schools" of #includestrategies. One says that header file must include everything that is needed for its own compilation. That would mean that GXGL.hand GXDX.hmust include GXRenderer.h. If you followed that strategy, your GXGL.cppand GXDX.cppwould be OK as they are now.

至少有两种#include策略“流派” 。有人说头文件必须包含它自己编译所需的一切。这意味着GXGL.h并且GXDX.h必须包括GXRenderer.h. 如果您遵循该策略,您的GXGL.cppGXDX.cpp现在就可以了。

Another "school" says that header files must not include each other at all, i.e. all inclusions must be done through .cpp files. At first sight one could guess that your GXGL.hand GXDX.hfollow that strategy (since you are not including anything into them), but then your GXRenderManager.hlooks completely different.

另一个“学校”说头文件不能相互包含,即所有的包含都必须通过 .cpp 文件来完成。乍一看,人们可能会猜到您GXGL.hGXDX.h遵循该策略(因为您没有在其中包含任何内容),但是您GXRenderManager.h看起来完全不同。

You need to decide which strategy you are trying to follow and follow it. I'd recommend the first one.

您需要决定要尝试遵循的策略并遵循它。我推荐第一个。

回答by Kumar

I got an error C2504: 'CView' : base class undefined where CView is not directly my base class from which I am inheriting.

我收到错误 C2504: 'CView' : base class undefined 其中 CView 不是我继承的直接基类。

I am inherting mYClass from MScrollView, "for this matter any class which is not actual Base Class is what the point is to be noted down here" but the error is the C2504. When I have included it in the header where this problem is arising, this problem is resolved.

我从 MScrollView 继承了 mYClass,“就此而言,任何不是实际基类的类都是这里要注意的重点”,但错误是 C2504。当我将它包含在出现此问题的标题中时,此问题就解决了。

#include "stdafx.h"

where stdafx.h has #include which contains all the basic class defined...hope this answer resolves everyone who are facing this issue.

其中 stdafx.h 有 #include ,其中包含定义的所有基本类...希望这个答案可以解决每个人都面临这个问题。