6

I wrote a little test program to show here. Here is the source code.

main.cpp:

#include "core.h"

Core core;

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

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

#ifndef DEFINITIONS_H__
#define DEFINITIONS_H__

class Core;
class Window;

#endif

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. 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 too long to post here.

ggorlen
  • 44,755
  • 7
  • 76
  • 106
roohan
  • 731
  • 3
  • 8
  • 15
  • 1
    Compiler is complaining about the constructor. Do you have the default constructor as part of the Core class if you have overloaded the constructor? – Mahesh Oct 27 '11 at 23:00
  • 6
    Hello, @user. Welcome to Stack Overflow. While someone may know the answer to your question as you present, the odds improve if you paste a **complete**, **minimal** program. Complete means that we can download it and compile it to see the compiler error ourselves. Minimal means that it is the smallest program that still demonstrates the error. Also, in creating such a program, you very well might find the error yourself! See http://sscce.org for more details. – Robᵩ Oct 27 '11 at 23:00

3 Answers3

8

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

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. This is called the Singleton pattern.

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

class Window
{
  void someFunction();
};

window.cpp

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

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

  core->doSomething();
}
vz0
  • 32,345
  • 7
  • 44
  • 77
  • Hi, and thank you for the answers. If I put my implemntation into a ccp file ill have the problem that my functions are inline and i cannot inline my functions in seperate cpp file. – roohan Oct 27 '11 at 23:08
  • @vz0: `undefined type 'Core::Core'` error will not be corrected by anything I see posted here. – Mooing Duck Oct 27 '11 at 23:29
  • Well, he clarified the error message, and now I see the (real) problem that you had addressed. Don't I feel foolish. – Mooing Duck Oct 28 '11 at 00:13
  • Hey vz0... Ok moving the function to a window.cpp file didnt fix it alone. After reading your update I saw that you included the core header in the window.cpp file and now it works. Thank you very much! The only problem is that i cant use this function inline now ... – roohan Oct 28 '11 at 00:52
  • Most linkers can inline it anyway. Don't worry about it. – Mooing Duck Oct 28 '11 at 01:06
  • I am getting `undefined reference to Core::coreInstance`. Any idea the reason? – Dipankar Saha Jun 16 '17 at 07:54
  • 1
    @DipankarSaha you;re missing the Core Core::coreInstance; in the .cpp file – vz0 Jun 20 '17 at 19:12
0

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

JKor
  • 3,822
  • 3
  • 28
  • 36
0

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. The solution is to do as vz0 suggested from the get go. Move the definition of Window::someFunction to window.cpp (and I feel the need to go apologize to vz0)

Community
  • 1
  • 1
Mooing Duck
  • 64,318
  • 19
  • 100
  • 158
  • Hi, I didnt have a default Constructor yet. But adding them didnt solve my problem. I still get the error that core is undeclared in the Window class. – roohan Oct 27 '11 at 23:37
  • Is `C2027: use of undefined type 'Core::Core'`(uppercase) the actual error message? or is it `C2027: use of undefined type 'Core::core'`(lowercase)? or `C2027: use of undefined type 'Core'`(noscope)? I assumed the first since that's what you wrote, but defining the constructors should have fixed that. – Mooing Duck Oct 27 '11 at 23:49
  • According to http://msdn.microsoft.com/en-us/library/6c2dk0ah(v=vs.80).aspx, the thing in quotes in the error should be a type. You listed a function. Is that really the error message? – Mooing Duck Oct 27 '11 at 23:53
  • Hi, i edited my first post to clarify my problem a bit I hope it helps. – roohan Oct 28 '11 at 00:02
  • When I move the definition of the function to a cpp file "Core" is still unkown. If I try to initilize Core as a pointer the compiler still complains that he doesnt know the Core Datatype in the window header file. – roohan Oct 28 '11 at 00:38