IT'S SIMPLIFIED CODE! I HAVE C++ FILES(WHERE IS IMPLEMENTATION) AND HEADERS FILES(WHERE IS CLASS DEFINITIONS)!
I have a file: Foo.cpp that incliudes main.h. And I have file Bar.cpp that uses funcs Foo.cpp and also includes main.h. It uses struct to access Foo object and call it's function. But struct defined in main.h? I tried to solve it like this:
**IN MAIN.H**
#pragma once
class Foo;
struct FoobarPackage {
FoobarPackage(Foo *fooObj) {
soso = fooObj;
}
Foo *soso;
};
* * *
**IN FOO.CPP**
#pragma once
#include "main.h"
class Foo {
void doSomething(bool ololo) {
if (ololo) //do something else
}
};
* * *
**IN BAR.CPP**
#pragma once
#include "main.h"
#include "Foo.cpp"
class Bar {
bool killAllHumans(FoobarPackage planet) {
planet.soso->doSomething(true);
return true;
}
};
* * *
But it cause:
Bar.cpp:8: error: invalid use of incomplete type "struct(WTF??!!! — author's comment) Foo"
main.h:3: error: forward declaration of "struct(why struct?) Foo"
What's wrong with my code? Also it's not real code. I simplified my real project and cut all that do not need. Foo.cpp and Bar.cpp of course have their headers where Foo and Bar classes defined and in .cpp files it's only their implementation. Also killAllHumans()
called from main.cpp where main()
located.
*EDITED*
I know that #include
works with headers but I wrote that it's "pseudocode". I use header file and cpp file in my readl project and include only headers and #pragma once
are in my headers. In this question I only simplify my code! Please read all question before answering!
*EDITED2* Tried to compile it now. It works. Strange.
Thanks.