0

Couldn't find a solution to this anywhere

here is my file layout

SDK.h

#pragma once
#include "Object.h"
#include "Panel.h"

Object.H

#pragma once
#include "SDK.h"

class BaseObject
{
etc...
}

Panel.h

#pragma once
#include "SDK.h"

class Panel : public BaseObject
{
etc...
}

and then the error

Panel.h(5): error C2504: 'BaseObject' : base class undefined

can somebody please tell me how to do it correctly? it's probably a very simple fix :(

EDIT: I looked and couldn't solve it, would I have to create a new file and include them in .CPP files?

  • Do you have semicolons after class definitions in your code? Also, you're running into circular include dependency with SDK.h and Object.h. – LogicStuff Aug 29 '15 at 22:04
  • possible duplicate of [Circular C++ Header Includes](http://stackoverflow.com/questions/1281641/circular-c-header-includes) – LogicStuff Aug 29 '15 at 22:06
  • yes i do, and still cant solve it – jacob harris Aug 29 '15 at 22:22
  • @LogicStuff `#pragma once` prevents multiple / circular inclusion – MikeMB Aug 29 '15 at 22:23
  • yup, did that too on all header files – jacob harris Aug 29 '15 at 22:25
  • @MikeMB [Another duplicate](http://stackoverflow.com/questions/10918250/pragma-once-and-include-issues) - `#pragma once` does not prevent circular inclusion – LogicStuff Aug 29 '15 at 22:27
  • @jacobharris: Pleas provide a [mcve](http://stackoverflow.com/help/mcve) – MikeMB Aug 29 '15 at 22:27
  • @MikeMB uhh just do the things i said and include SDK.h in main – jacob harris Aug 29 '15 at 22:30
  • @LogicStuff:`pragma one` DOES prevent circular **inclusion**. The problem in the linked question is circular **dependency**, which you have to resolve by forward declaration. – MikeMB Aug 29 '15 at 22:33
  • @MikeMB OK, I used wrong terms. But the dupplicate I proposed is nonetheless right. – LogicStuff Aug 29 '15 at 22:34
  • @LogicStuff: What I wanted to say is that it is a different problem - irrespective of what you call it. There is no circular dependency in the code jacob shows - only a very bad include strategy that - depending on the way he included the files in the source files - will blow up or not. Thats why I asked for an actual mcve.. – MikeMB Aug 29 '15 at 22:40
  • @jacobharris: Aside from the fact, that I agree with Dietmar's guess, I'm to lazy to create a bunch of files, copy your code and correct your spelling mistakes just to see that it can't reproduce your problem because your question lacks the necessary details. If you want help, you should be willing to invest at least a minimal amount of work. – MikeMB Aug 29 '15 at 22:51

2 Answers2

1

You didn't include any information about the translation unit failing to compile the code. Thus, I'm speculating. My guess is that you have something including Object.h first:

  • the compiler starts including Object.h and marks it to not include again
  • the compiler includes SDK.h, suspending inclusion of Object.h and marks it to not include again
  • inclusion of Object.h is skipped
  • the compiler includes Panel.h, suspending inclusion of SDK.h and marks it to not include again
  • inclusion of SDK.h is skipped
  • the definition of Panel is reached but BaseObject is not defined, yet, and you get the error

The fix is not to include SDK.h from either Object.h or Panel.h. Instead Panel.h should include Object.h and both headers should include whatever else they need.

Dietmar Kühl
  • 150,225
  • 13
  • 225
  • 380
  • i see, but if i wanted to add like Button.h what would i do then? – jacob harris Aug 30 '15 at 01:37
  • @jacobharris: you'd include the header(s) defining its direct base class(es) as well as headers for everything it needs declarations for. If you want to provide a convenience header for users of the library you'd include the newly created header there, too. – Dietmar Kühl Aug 30 '15 at 08:55
0
  • Remove #include "SDK.H" from Object.h

  • [Last resort]If problem still doesn't solves , use header guards , here is the tutorial for it.

Community
  • 1
  • 1
  • Did you actually check this? If so, can you explain, why this helps? – MikeMB Aug 29 '15 at 22:39
  • simple base class is declaring more than one time , so remove it . i know pragma is there , but in this particular case it don't work. –  Aug 29 '15 at 22:56
  • While I guess that your suggestion will probably work, I'm pretty sure, the explanation is wrong, because a) that would produce a different compiler error and b) mean that the compiler would not handle `pragma once` correctly, which - i think - is very unlikely. – MikeMB Aug 29 '15 at 23:09