Note that there are some explanatory texts on larger screens.

plurals
  1. PONamespaces and includes generate link error
    text
    copied!<p>I was playing around with namespaces when I encountered a lnk2005 error. I can't figure out how to get around the error. Here's the error:</p> <pre><code>1&gt;Source.obj : error LNK2005: "int Chart::Bars::d" (?d@Bars@Chart@@3HA) already defined in Chart.obj 1&gt;Source.obj : error LNK2005: "class foo Chart::l" (?l@Chart@@3Vfoo@@A) already defined in Chart.obj 1&gt;Source.obj : error LNK2005: "int Chart::t" (?t@Chart@@3HA) already defined in Chart.obj 1&gt;C:\Users\bnm\dev\examples\play\nmspca\Debug\nmspca.exe : fatal error LNK1169: one or more multiply defined symbols found 1&gt; 1&gt;Build FAILED. 1&gt; 1&gt;Time Elapsed 00:00:00.49 ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ========== </code></pre> <p>Here's the code...</p> <p>Chart.h</p> <pre><code>#pragma once #include "foo.h" namespace Chart { int t; foo l; namespace Bars { int d; } } </code></pre> <p>Foo.h</p> <pre><code>#pragma once class foo { public: int ss; char* h; }; </code></pre> <p>Chart.cpp</p> <pre><code>#include "Chart.h" using namespace Chart; int main () { l.h = "g"; } </code></pre> <p>Source.cpp</p> <pre><code>#include "Chart.h" using namespace Chart; int test() { l.ss = 0; return l.ss; } </code></pre> <p>When the #include "Chart.h" from Source.cpp is removed the problems goes away. However, Source.cpp needs #include "Chart.h" for the namespace definition.</p> <p>What's the correct way to express that "namespace Chart" is needed in both Chart.cpp and Source.cpp so that everything compiles?</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