Note that there are some explanatory texts on larger screens.

plurals
  1. PODefining trouble. C++
    text
    copied!<p><strong>IT'S SIMPLIFIED CODE! I HAVE C++ FILES(WHERE IS IMPLEMENTATION) AND HEADERS FILES(WHERE IS CLASS DEFINITIONS)!</strong></p> <p>I have a file: <strong>Foo.cpp</strong> that incliudes <strong>main.h</strong>. And I have file <strong>Bar.cpp</strong> that uses funcs <strong>Foo.cpp</strong> and also includes <strong>main.h</strong>. It uses struct to access Foo object and call it's function. But struct defined in <strong>main.h</strong>? I tried to solve it like this:</p> <pre><code>**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-&gt;doSomething(true); return true; } }; * * * </code></pre> <p>But it cause:</p> <pre><code>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" </code></pre> <p>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 <strong>.cpp</strong> files it's only their implementation. Also <code>killAllHumans()</code> called from main.cpp where <code>main()</code> located.</p> <p>*<strong><em>EDITED</em>*</strong> I know that <code>#include</code> 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 <code>#pragma once</code> are in my headers. In this question I only simplify my code! Please read all question before answering!</p> <p>*<strong><em>EDITED2</em>*</strong> Tried to compile it now. It works. Strange.</p> <p>Thanks.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload