This is my code and I am getting a linking problem on the constructor:  #ifndef LOCKFREEQUEUE_H #define LOCKFREEQUEUE_H  #include &lt;atomic&gt; #include &lt;memory&gt;  template&lt;typename T&gt; class lock_free_queue {     private:         struct node         {             std::shared_ptr&lt;T&gt; data;             node* next;             node():next(nullptr){}         };          std::atomic&lt;node*&gt; head;         std::atomic&lt;node*&gt; tail;          node* pop_head()         {             node* const old_head=head.load();             if(old_head==tail.load())             {                 return nullptr;             }             head.store(old_head-&gt;next);             return old_head;         }          lock_free_queue(const lock_free_queue&amp; other);         lock_free_queue&amp; operator=(const lock_free_queue&amp; other);      public:         lock_free_queue();         ~lock_free_queue();         std::shared_ptr&lt;T&gt; pop();         void push(T new_value); };   Source file:  #include "lock_free_queue.h"  template&lt;typename T&gt; lock_free_queue&lt;T&gt;::lock_free_queue(const lock_free_queue&amp; other) { }  template&lt;typename T&gt; lock_free_queue&lt;T&gt;&amp; lock_free_queue&lt;T&gt;::operator=(const lock_free_queue&amp; other) { }  template&lt;typename T&gt; lock_free_queue&lt;T&gt;::lock_free_queue():head(new node),tail(head.load()) { }  template&lt;typename T&gt; lock_free_queue&lt;T&gt;::~lock_free_queue() {     while(node* const old_head=head.load())     {         head.store(old_head-&gt;next);         delete old_head;     } }  template&lt;typename T&gt; std::shared_ptr&lt;T&gt; lock_free_queue&lt;T&gt;::pop() {     node* old_head=pop_head();     if(!old_head)     {         return std::shared_ptr&lt;T&gt;();     }     std::shared_ptr&lt;T&gt; const res(old_head-&gt;data);     delete old_head;     return res; }  template&lt;typename T&gt; void lock_free_queue&lt;T&gt;::push(T new_value) {     std::shared_ptr&lt;T&gt; new_data(std::make_shared&lt;T&gt;(new_value));     node* p=new node;     node* const old_tail=tail.load();     old_tail-&gt;data.swap(new_data);     old_tail-&gt;next=p;     tail.store(p); }   Main:  #include "lock_free_queue.h"  int main(){     lock_free_queue&lt;int&gt;* my_queue = new lock_free_queue&lt;int&gt;();     my_queue-&gt;push(3);     return 1; }   Linker error:  Main.obj : error LNK2019: unresolved external symbol "public: __cdecl lock_free_queue&lt;int&gt;::lock_free_queue&lt;int&gt;(void)" (??0?$lock_free_queue@H@@QEAA@XZ) referenced in function main Main.obj : error LNK2019: unresolved external symbol "public: void __cdecl lock_free_queue&lt;int&gt;::push(int)" (?push@?$lock_free_queue@H@@QEAAXH@Z) referenced in function main   I can't see why these aren't linking?
341
Quote from :     The only portable way of using templates at the moment is to implement them in header files by using inline functions.   Why is this?  (Clarification: header files are not the only portable solution. But they are the most convenient portable solution.)
48447
recently I've been reading through Scott Meyers's excellent  book. In one of the last tips he covered some of the features from TR1 - I knew many of them via Boost.   However, there was one that I definitely did NOT recognize: tr1::reference_wrapper.   How and when would I use tr1::reference_wrapper?
19841
I've been programming in C++ for a few years, and I've used STL quite a bit and have created my own template classes a few times to see how it's done.  Now I'm trying to integrate templates deeper into my OO design, and a nagging thought keeps coming back to me: They're just a macros, really... You could implement (rather UGLY) auto_ptrs using #defines, if you really wanted to.  This way of thinking about templates helps me understand how my code will actually work, but I feel that I must be missing the point somehow. Macros are meant evil incarnate, yet "template metaprogramming" is all the rage.  So, what ARE the real distinctions? and how can templates avoid the dangers that #define leads you into, like   Inscrutable compiler errors in places where you don't expect them? Code bloat?  Difficulty in tracing code? Setting Debugger Breakpoints?
36051
class Test {        bool isVal() const {           return isVal;       }    private:        bool isVal; };   On Compiling this file it says      testClass.cpp:9: declaration of `bool   Test::isVal'      testClass.cpp:3: conflicts with   previous declaration `bool   Test::isVal()'   Although the same would work for java   class Test {    private boolean isVal;    public boolean isVal() {       return isVal;   }  }   Why does the compile error occur in C++ not in Java?
12470
Why do we need to use:  extern "C" { #include &lt;foo.h&gt; }   Specifically:     When should we use it? What is happening at the compiler/linker level that requires us to use it?   How in terms of compilation/linking does this solve the problems which require us to use it?
32521
I came across this strange code snippet which compiles fine:  class Car {     public:     int speed; };  int main() {     int Car::*pSpeed = &amp;Car::speed;     return 0; }   Why does C++ have this pointer to a non-static data member of a class? What is the use of this strange pointer in real code?
92781
For a simple linked list in which random access to list elements is not a requirement, are there any significant advantages (performance or otherwise) to using std::list instead of std::vector?  If backwards traversal is required, would it be more efficient to use std::slist and reverse() the list prior to iterating over its elements?
1257
I'm currently refactoring/tidying up some old C code used in a C++ project, and regularly see functions such as:  int f(void)   which I would tend to write as:  int f()   Is there any reason not to replace (void) with () throughout the codebase in order to improve consistency, or is there a subtle difference between the two that I am unaware of?  More specifically, if a virtual member function in C++ is described as:  virtual int f(void)   and a derived class includes a member function:  int f()   is this a valid override?  Additionally, am I likely to encounter any linker problems based on almost identical signatures?
21989
Very simply put:  I have a class that consists mostly of static public members, so I can group similar functions together that still have to be called from other classes/functions.  Anyway, I have defined two static unsigned char variables in my class public scope, when I try to modify these values in the same class' constructor, I am getting an "unresolved external symbol" error at compilation.  class test  { public:     static unsigned char X;     static unsigned char Y;      ...      test(); };  test::test()  {     X = 1;     Y = 2; }   I'm new to C++ so go easy on me. Why can't I do this?
109393
Consider the following code:  template&lt;class T&gt; class Foo { public:   Foo() { a = 1; }  protected:   int a; };  template&lt;class T&gt; class Bar : public Foo&lt;T&gt; { public:   Bar() { b = 4; };    int Perna(int u);  protected:   int b;  };  template&lt;class T&gt; int Bar&lt;T&gt;::Perna(int u)  {    int c = Foo&lt;T&gt;::a * 4; // This works   return (a + b) * u;    // This doesn't }   g++ 3.4.6, 4.3.2 and 4.1.2 give error   test.cpp: In member function `int Bar&lt;T&gt;::Perna(int)': test.cpp:25: error: `a' was not declared in this scope   g++ 2.96 and MSVC 6, 7, 7.1, 8 and 9 accept it, as do (at least) older Intel and SGI c++ compilers.   Do the new Gnu C++ compiler obey the standard or not? If they do, what is the rationale behind that inheriting class is not able to see a protected inherited member variable?  Also, if there's  int A() { return a; }    in Foo, I get error  test.cpp:25: error: there are no arguments to A that depend on a template parameter, so a declaration of A must be available test.cpp:25: error: (if you use -fpermissiveâ, G++ will accept your code, but allowing the use of an undeclared name is deprecated)   when I try to use it in a member function of Bar. I find that curious as well: Bar inherits Foo, so I think it's obvious that A() in scope of Bar is Foo::A().
107003
I'm working on a sparse matrix class that needs to use an array of LinkedList to store the values of a matrix. Each element of the array (i.e. each LinkedList) represents a row of the matrix. And, each element in the LinkedList array represents a column and the stored value.  In my class, I have a declaration of the array as:  private LinkedList&lt;IntegerNode&gt;[] myMatrix;   And, in my constructor for the SparseMatrix, I try to define:  myMatrix = new LinkedList&lt;IntegerNode&gt;[numRows];   The error I end up getting is     Cannot create a generic array of LinkedList&lt;IntegerNode&gt;.   So, I have two issues with this:   What am I doing wrong, and Why is the type acceptable in the declaration for the array if it can't be created?   IntegerNode is a class that I have created. And, all of my class files are packaged together.
3226
class String {      private:         char* rep;      public:         String (const char*);         void toUpper() const; };   String :: String (const char* s) {     rep = new char [strlen(s)+1];     strcpy (rep, s); }   void String :: toUpper () const {     for (int i = 0; rep [i]; i++)     rep[i] = toupper(rep[i]); }   int main () {     const String lower ("lower");     lower.toUpper();      cout &lt;&lt; lower &lt;&lt; endl;     return 0; }
18652
I have the following class in C++:  class a {     const int b[2];     // other stuff follows      // and here's the constructor     a(void); }   The question is, how do I initialize b in the initialization list, given that I can't initialize it inside the body of the function of the constructor, because b is const?  This doesn't work:  a::a(void) :      b([2,3]) {      // other initialization stuff }   Edit:  The case in point is when I can have different values for b for different instances, but the values are known to be constant for the lifetime of the instance.
4123
I just ran across the following error (and found the solution online, but it's not present in Stack Overflow):     (.gnu.linkonce.[stuff]): undefined   reference to [method] [object   file]:(.gnu.linkonce.[stuff]):   undefined reference to `typeinfo for   [classname]'   Why might one get one of these "undefined reference to typeinfo" linker errors?  (Bonus points if you can explain what's going on behind the scenes.)
94655
I was experimenting with C++ and found the below code as very strange.   class Foo{ public:     virtual void say_virtual_hi(){         std::cout &lt;&lt; "Virtual Hi";     }      void say_hi()     {         std::cout &lt;&lt; "Hi";     } };  int main(int argc, char** argv) {     Foo* foo = 0;     foo-&gt;say_hi(); // works well     foo-&gt;say_virtual_hi(); // will crash the app     return 0; }   I know that the virtual method call crashes because it requires a vtable lookup and can only work with valid objects.   I have the following questions    How does the non virtual method say_hi work on a NULL pointer? Where does the object foo get allocated?   Any thoughts?
7208
Is it possible to forward declare an standard container in a header file?  For example, take the following code:  #include &lt;vector&gt;  class Foo { private:     std::vector&lt;int&gt; container_;     ... };   I want to be able to do something like this:  namespace std {     template &lt;typename T&gt; class vector; }  class Foo { private:     std::vector&lt;int&gt; container_;     ... };   Can this be done?
15868
Why does this code:  class A {     public:          explicit A(int x) {} };  class B: public A { };  int main(void) {     B *b = new B(5);     delete b; }   Result in these errors:   main.cpp: In function ‘int main()’: main.cpp:13: error: no matching function for call to ‘B::B(int)’ main.cpp:8: note: candidates are: B::B() main.cpp:8: note:                 B::B(const B&)   Shouldn't B inherit A's constructor?  (this is using gcc)