C++ 外部类声明

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

C++ Extern Class Declaration

c++

提问by roohan

Edit: Ok I wrote a little test program to show here. Here is the Source Code.

编辑:好的,我写了一个小测试程序在这里展示。这是源代码。

main.cpp:

主.cpp:

#include "core.h"

Core core;

int main()
{
  core.coreFunction();
}

core.h:

核心.h:

#ifndef CORE_H__
#define CORE_H__

#include "definitions.h"
#include "window.h"

class Core
{
public:
  Window window;
  void coreFunction()
  {
    window.windowFunction();
  }
};
extern Core core;

#endif

definitions.h

定义.h

#ifndef DEFINITIONS_H__
#define DEFINITIONS_H__

class Core;
class Window;

#endif

window.h

窗口.h

#ifndef WINDOW_H__
#define WINDOW_H__

class Window
{
public:

   void windowFunction()
   {
     core.coreFunction();
   }
};

#endif

With this Test Program I get the following Error: window.h(10): error C2065: 'core' : undeclared identifier. I hope this clarifies my problem a little bit. Please ignore that these functions make no sense its just for showing what i did because my original code is way to long to post here.

使用此测试程序,我得到以下错误:window.h(10): error C2065: 'core' : undeclared identifier。我希望这能稍微澄清我的问题。请忽略这些函数只是为了展示我所做的没有意义,因为我的原始代码在这里发布太久了。

回答by vz0

You are including the window.hheader before the "extern Core core;" line. Try adding that line just before the class Windowline on the window.hheader:

window.h在“ extern Core core;”行之前包含标题。尝试class Windowwindow.h标题行之前添加该行:

window.h

窗口.h

#ifndef WINDOW_H__
#define WINDOW_H__

extern Core core;

class Window
{...}

Instead of using Core as a global variable, you can move coreas a static member of the Coreclass. This is called the Singleton pattern.

您可以core作为类的静态成员移动,而不是使用 Core 作为全局变量Core。这称为单例模式

main.cpp

主程序

#include "core.h"

int main()
{
  Core* core = Core::getInstance();

  core->coreFunction();
}

core.h

核心文件

#include "window.h"

class Core
{
public:
  static Core* getInstance() { return &coreInstance; }
  void someFunction();

private:
  static Core coreInstance;
  Window window;
};

core.cpp

核心文件

#include "core.h"

Core Core::coreInstance;

void Core::someFunction()
{
  window.doSomething();
}

window.h

窗口.h

class Window
{
  void someFunction();
};

window.cpp

窗口.cpp

#include "window.h"
#include "core.h"

void Window::someFunction()
{
  Core* core = Core::getInstance();

  core->doSomething();
}

回答by JKor

I think I found your problem. In the definition header file the "Core" class is declared as "core". Remember, caps makes a big difference.

我想我发现了你的问题。在定义头文件中,“Core”类被声明为“core”。请记住,上限有很大的不同。

回答by Mooing Duck

Either you forgot to define core's default constructor, or core cannot be trivially default constructed (due to having a base or member that does not have a default constructor.

要么你忘记定义 core 的默认构造函数,要么 core 不能被简单地默认构造(因为有一个没有默认构造函数的基类或成员。

To core, add:

核心,添加:

class Core {
    Window window;

    Core() {} //add this line
    void someFunction()
    {
        window.doSomething();
    }
}

To window, add:

要窗口,添加:

class Window
{
    Window() {} //add this line
    void someFunction()
    {
        core.doSomething();
    }
}

If either fails to compile, you'll have pinned down the problem a little more

如果其中任何一个都无法编译,您就会更加确定问题

EDIT:

编辑:

Well now that the error message was clarified, I see the error right off. Window.hrequires Core coreto be defined, and Core.hrequires Windowto be defined. The solution is to do as vz0 suggested from the get go. Move the definition of Window::someFunctionto window.cpp(and I feel the need to go apologize to vz0)

既然错误消息已经澄清,我马上就看到了错误。 Window.h需要Core core被定义,也Core.h需要Window被定义。解决方案是按照 vz0 从一开始就建议。移动Window::someFunctionto的定义window.cpp(我觉得有必要向 vz0 道歉)