简体   繁体   English

C ++外部类声明

[英]C++ Extern Class Declaration

Edit: Ok I wrote a little test program to show here. 编辑:好的我写了一个小测试程序来显示在这里。 Here is the Source Code. 这是源代码。

main.cpp: main.cpp中:

#include "core.h"

Core core;

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

core.h: core.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 definitions.h

#ifndef DEFINITIONS_H__
#define DEFINITIONS_H__

class Core;
class Window;

#endif

window.h 在window.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 . 使用此测试程序,我得到以下错误: window.h(10):错误C2065:'core':未声明的标识符 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. 请忽略这些函数对于显示我所做的事情没有任何意义,因为我的原始代码是在此处发布的方式。

You are including the window.h header before the " extern Core core; " line. 您将在“ extern Core core; ”行之前包含window.h标头。 Try adding that line just before the class Window line on the window.h header: 尝试在window.h标题的class Window行之前添加该行:

window.h 在window.h

#ifndef WINDOW_H__
#define WINDOW_H__

extern Core core;

class Window
{...}

Instead of using Core as a global variable, you can move core as a static member of the Core class. 您可以将core作为Core类的静态成员移动,而不是将Core用作全局变量。 This is called the Singleton pattern . 这称为Singleton模式

main.cpp main.cpp中

#include "core.h"

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

  core->coreFunction();
}

core.h core.h

#include "window.h"

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

private:
  static Core coreInstance;
  Window window;
};

core.cpp core.cpp

#include "core.h"

Core Core::coreInstance;

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

window.h 在window.h

class Window
{
  void someFunction();
};

window.cpp window.cpp

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

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

  core->doSomething();
}

I think I found your problem. 我想我找到了你的问题。 In the definition header file the "Core" class is declared as "core". 在定义头文件中,“Core”类被声明为“core”。 Remember, caps makes a big difference. 请记住,上限会产生很大的不同。

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. 要么你忘了定义核心的默认构造函数,要么核心不能简单地默认构造(由于有一个基础或成员没有默认的构造函数)。

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.h requires Core core to be defined, and Core.h requires Window to be defined. Window.h需要定义Core coreCore.h需要定义Window The solution is to do as vz0 suggested from the get go. 解决方案是从get go开始建议vz0。 Move the definition of Window::someFunction to window.cpp (and I feel the need to go apologize to vz0) Window::someFunction的定义移动到window.cpp (我觉得需要向vz0道歉)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM