Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<h1>Problem</h1> <p>You should not <code>#pragma once</code> in source files, and you shall not <code>#include</code> source files into each other. </p> <p>The problem in your case (probably, but your test-case is not exact): You <code>#include "Foo.cpp"</code>, but at that moment, the compiler has already triggered upon <code>#pragma once</code> long before when compiling <code>Foo.cpp</code>.</p> <p>In simple terms: <strong>The compiler has been confused because you mix up source-files with header-files.</strong></p> <h1>Canonical modus operandi</h1> <ul> <li><p>A header/source pair for each class</p> <ul> <li><p>the header generally only has declarations, e.g. <code>class Foo { void someMethod(); };</code></p></li> <li><p>the source includes the header and defines the class members, e.g. <code>void Foo::someMethod() {...}</code></p></li> </ul></li> <li><p>Other translation units ("source files") can then happily <code>#include</code> the header</p></li> </ul> <h1>Example</h1> <p><strong>foo.h</strong></p> <pre><code>#ifndef FOO_H #define FOO_H #pragma once // note: compiler extension, do as // you prefer, advantage of #pragma // once is near to nothing on today's // compilers, though class Foo { public: void someMethod(); }; #endif </code></pre> <p><strong>foo.cpp</strong></p> <pre><code>#include "foo.h" void Foo::someMethod() { // do something } </code></pre> <p>Then, you can use this in other translation units like this:</p> <p><strong>main.cpp</strong></p> <pre><code>#include "foo.h" int main () { Foo foo; foo.doSomething(); } </code></pre>
 

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