Note that there are some explanatory texts on larger screens.

plurals
  1. POLinking error in dereferencing static variable
    primarykey
    data
    text
    <p>(I've failed to find any similar question... so, I hope you can help me)</p> <p>In a program in C++ that I'm developing, I have a class that simulates a thread. I'll call it "Test" here. In it, I have an static map (<code>std::map</code>, from STL) that in which I store some semaphores (because I need all of the threads to have access to the same semaphores). (I think it is not worth to explain why I'm using a <code>map</code>, instead of a <code>vector</code>, but I believe this shouldn't be a problem)</p> <p>To "get" this static variable, I created a <code>getMutexHash()</code> function, that returns a pointer to the static <code>map</code>. But, for some reason, after compiling, I'm getting a linker error when trying to return a <code>this pointer</code>.</p> <p>The following code exemplifies the problem:</p> <pre class="lang-cpp prettyprint-override"><code>// MAIN.CPP #include "Test.h" int main () { Test test; map&lt;int, pthread_mutex_t&gt;* mutexHash = test.getMutexHash(); return 0; } // TEST.H #include &lt;map&gt; #include &lt;pthread.h&gt; using namespace std; class Test { public: map&lt;int, pthread_mutex_t&gt;* getMutexHash(); private: static map&lt;int, pthread_mutex_t&gt; mutexHash; }; // TEST.CPP #include "Test.h" map&lt;int, pthread_mutex_t&gt;* Test::getMutexHash() { return &amp;mutexHash; } </code></pre> <p>When compiling, I get no error nor warnings; but when linking, I receive this error:</p> <pre><code>Test.o: In function `Test::getMutexHash()': Test.cpp:(.text+0x9): undefined reference to `Test::mutexHash' collect2: ld returned 1 exit status </code></pre> <p>Can someone help me?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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