I defined a new class SafeQueue but when I import it and link it to my program, I get undefined reference error. safe_queue.h: #pragma once #include &lt;queue&gt; #include &lt;mutex&gt; #include &lt;condition_variable&gt; using namespace std;  template&lt;class T&gt; class SafeQueue {   public:     SafeQueue();     ~SafeQueue();     void enqueue(T t);     T dequeue();   private:     queue&lt;T&gt; q;     mutable mutex m;     condition_variable c; };  safe_queue.cc: #include &lt;queue&gt; #include &lt;mutex&gt; #include &lt;condition_variable&gt; #include &quot;safe_queue.h&quot;  using namespace std;  // A threadsafe-queue. template &lt;class T&gt; SafeQueue&lt;T&gt;::SafeQueue() : q(), m(), c() {}  template &lt;class T&gt; SafeQueue&lt;T&gt;::~SafeQueue() {}  // Add an element to the queue. template &lt;class T&gt; void SafeQueue&lt;T&gt;::enqueue(T t) {   lock_guard&lt;mutex&gt; lock(m);   q.push(t);   c.notify_one(); }  // Get the &quot;front&quot;-element. // If the queue is empty, wait till a element is avaiable. template &lt;class T&gt; T SafeQueue&lt;T&gt;::dequeue() {   unique_lock&lt;mutex&gt; lock(m);   while(q.empty()) {     // release lock as long as the wait and reaquire it afterwards.     c.wait(lock);   }   T val = q.front();   q.pop();   return val; }  Error: g++ ../logger/logger.o ../protos/backend.grpc.pb.o ../protos/keyvaluestore.grpc.pb.o ../protos/backend.pb.o ../protos/console.grpc.pb.o ../protos/keyvaluestore.pb.o ../protos/console.pb.o globals/globals.o grpc/backend_client.o grpc/console_client.o grpc/backend_server.o grpc/keyvaluestore_server.o grpc/console_server.o file_reader_functions/password_reader.o file_reader_functions/file_writer.o file_reader_functions/queries_writer.o pack_addrs/pack_addrs.o safe_queue/safe_queue.o master_node.o master_helper_functions/startup.o -L/usr/local/lib `pkg-config --libs protobuf grpc++` -lpthread -Wl,--no-as-needed -lgrpc++_reflection -Wl,--as-needed -ldl -lz -o master_node /usr/bin/ld: globals/globals.o: in function `__static_initialization_and_destruction_0(int, int)': globals.cc:(.text+0x185): undefined reference to `SafeQueue&lt;std::__cxx11::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt; &gt;::SafeQueue()' /usr/bin/ld: globals.cc:(.text+0x19a): undefined reference to `SafeQueue&lt;std::__cxx11::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt; &gt;::~SafeQueue()' /usr/bin/ld: grpc/console_server.o: in function `ConsoleServiceImpl::JoinNetwork(grpc::ServerContext*, keyvaluestore::Request const*, console::ClusterInfo*)': console_server.cc:(.text+0x428): undefined reference to `SafeQueue&lt;std::__cxx11::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt; &gt;::enqueue(std::__cxx11::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt;)' /usr/bin/ld: master_helper_functions/startup.o: in function `RunHealthChecks()': startup.cc:(.text+0x483): undefined reference to `SafeQueue&lt;std::__cxx11::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt; &gt;::dequeue()' collect2: error: ld returned 1 exit status
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.)
63712
I think in this case there is no need to declare a public constructor since the class is not accessible outside the package anyway. But is there some hidden impact when the class has only package private constructor?
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?
84853
Comment on Duplicate Reference: Why would this be marked duplicate when it was asked years prior to the question referenced as a duplicate?  I also believe the question, detail, and response is much better than the referenced question.  I've been a C++ programmer for quite a while but I'm new to Java and new to Eclipse.  I want to use the  to visualize some data I'm working with.  This code is organized like this:  ./com ./com/touchgraph ./com/touchgraph/graphlayout ./com/touchgraph/graphlayout/Edge.java ./com/touchgraph/graphlayout/GLPanel.java ./com/touchgraph/graphlayout/graphelements ./com/touchgraph/graphlayout/graphelements/GESUtils.java ./com/touchgraph/graphlayout/graphelements/GraphEltSet.java ./com/touchgraph/graphlayout/graphelements/ImmutableGraphEltSet.java ./com/touchgraph/graphlayout/graphelements/Locality.java ./com/touchgraph/graphlayout/graphelements/TGForEachEdge.java ./com/touchgraph/graphlayout/graphelements/TGForEachNode.java ./com/touchgraph/graphlayout/graphelements/TGForEachNodePair.java ./com/touchgraph/graphlayout/graphelements/TGNodeQueue.java ./com/touchgraph/graphlayout/graphelements/VisibleLocality.java ./com/touchgraph/graphlayout/GraphLayoutApplet.java ./com/touchgraph/graphlayout/GraphListener.java ./com/touchgraph/graphlayout/interaction ./com/touchgraph/graphlayout/interaction/DragAddUI.java ./com/touchgraph/graphlayout/interaction/DragMultiselectUI.java ./com/touchgraph/graphlayout/interaction/DragNodeUI.java ./com/touchgraph/graphlayout/interaction/GLEditUI.java ./com/touchgraph/graphlayout/interaction/GLNavigateUI.java ./com/touchgraph/graphlayout/interaction/HVRotateDragUI.java ./com/touchgraph/graphlayout/interaction/HVScroll.java ./com/touchgraph/graphlayout/interaction/HyperScroll.java ./com/touchgraph/graphlayout/interaction/LocalityScroll.java ./com/touchgraph/graphlayout/interaction/RotateScroll.java ./com/touchgraph/graphlayout/interaction/TGAbstractClickUI.java ./com/touchgraph/graphlayout/interaction/TGAbstractDragUI.java ./com/touchgraph/graphlayout/interaction/TGAbstractMouseMotionUI.java ./com/touchgraph/graphlayout/interaction/TGAbstractMousePausedUI.java ./com/touchgraph/graphlayout/interaction/TGSelfDeactivatingUI.java ./com/touchgraph/graphlayout/interaction/TGUIManager.java ./com/touchgraph/graphlayout/interaction/TGUserInterface.java ./com/touchgraph/graphlayout/interaction/ZoomScroll.java ./com/touchgraph/graphlayout/LocalityUtils.java ./com/touchgraph/graphlayout/Node.java ./com/touchgraph/graphlayout/TGAbstractLens.java ./com/touchgraph/graphlayout/TGException.java ./com/touchgraph/graphlayout/TGLayout.java ./com/touchgraph/graphlayout/TGLensSet.java ./com/touchgraph/graphlayout/TGPaintListener.java ./com/touchgraph/graphlayout/TGPanel.java ./com/touchgraph/graphlayout/TGPoint2D.java ./com/touchgraph/graphlayout/TGScrollPane.java ./TG-APACHE-LICENSE.txt ./TGGL ReleaseNotes.txt ./TGGraphLayout.html ./TGGraphLayout.jar   How do I add this project in Eclipse and get it compiling and running quickly?
60697
An official spec for  with reference implementation was released recently.   Will these specs (with optional additions like MathJax for those sites that already have it) be adopted? There are some places where the implementation deviates,  [link](foo(and(bar))) amongst others.   Or will it break too many posts?
7545
Due to the implementation of Java generics, you can't have code like this:   public class GenSet&lt;E&gt; {     private E a[];      public GenSet() {         a = new E[INITIAL_ARRAY_LENGTH]; // error: generic array creation     } }   How can I implement this while maintaining type safety?  I saw a solution on the Java forums that goes like this:  import java.lang.reflect.Array;  class Stack&lt;T&gt; {     public Stack(Class&lt;T&gt; clazz, int capacity) {         array = (T[])Array.newInstance(clazz, capacity);     }      private final T[] array; }   But I really don't get what's going on.
29851
I have an error that seems common on the web,  Though i cant find a definite answer that applies to my case:  I have the following Apex code:  public with sharing class templicenseemail {      private ApexPages.standardController standardController;       public Trainee__c trainee { get; set; }     public templicenseemail(ApexPages.StandardController controller) {         this.standardController = standardController;     }      public PageReference doSomething()     {         trainee  = [SELECT Email__c,First_Name__c,Institution__c,Last_Name__c FROM Trainee__c                  WHERE Id = :ApexPages.currentPage().getParameters().get('id')];                  String whatIs = trainee.Volume_Serial_Number__c;                 integer strlen = whatIs.length();                  ApexPages.addMessage(new ApexPages.message(ApexPages.severity.INFO, trainee.Email__c  ));         return null;      }     }   And the following test case:  @isTest(seealldata=false)      private class Send_noti_emailsTestClass5 {          static testMethod void validateSend_noti_emails5() {              templicenseemail  MyTestcase = new templicenseemail();              // code here in the Middle              MyTestcase.doSomething();          }     }   I'm getting the following error:  Error: Compile Error: Constructor not defined: [templicenseemail].&lt;Constructor&gt;(SOBJECT:Lead) at line:      templicenseemail  MyTestcase = new templicenseemail();   Does anyone know why?
65547
See also      Not a major issue, just annoying as I don't want my class to ever be instantiated without the particular arguments.  #include &lt;map&gt;  struct MyClass {     MyClass(int t); };  int main() {     std::map&lt;int, MyClass&gt; myMap;     myMap[14] = MyClass(42); }   This gives me the following g++ error:     /usr/include/c++/4.3/bits/stl_map.h:419: error: no matching function for call to ‘MyClass()’   This compiles fine if I add a default constructor; I am certain it's not caused by incorrect syntax.
28775
Is it better to initialize class member variables on declaration  private List&lt;Thing&gt; _things = new List&lt;Thing&gt;(); private int _arb = 99;   or in the default constructor?  private List&lt;Thing&gt; _things; private int _arb;  public TheClass() {   _things = new List&lt;Thing&gt;();   _arb = 99; }   Is it simply a matter of style or are there performance trade-offs, one way or the other?
48824
I have a Queue&lt;T&gt; object that I have initialised to a capacity of 2, but obviously that is just the capacity and it keeps expanding as I add items.  Is there already an object that automatically dequeues an item when the limit is reached, or is the best solution to create my own inherited class?
983
I have some template code that I would prefer to have stored in a CPP file instead of inline in the header.  I know this can be done as long as you know which template types will be used.  For example:  .h file  class foo { public:     template &lt;typename T&gt;     void do(const T&amp; t); };   .cpp file  template &lt;typename T&gt; void foo::do(const T&amp; t) {     // Do something with t }  template void foo::do&lt;int&gt;(const int&amp;); template void foo::do&lt;std::string&gt;(const std::string&amp;);   Note the last two lines - the foo::do template function is only used with ints and std::strings, so those definitions mean the app will link.  My question is - is this a nasty hack or will this work with other compilers/linkers? I am only using this code with VS2008 at the moment but will be wanting to port to other environments.
14535
I am looking for the definition of when I am allowed to do forward declaration of a class in another class's header file:  Am I allowed to do it for a base class, for a class held as a member, for a class passed to member function by reference, etc. ?
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?
1488
Can anyone explain why following code won't compile? At least on g++ 4.2.4.  And more interesting, why it will compile when I cast MEMBER to int?  #include &lt;vector&gt;  class Foo {   public:       static const int MEMBER = 1;   };  int main(){       vector&lt;int&gt; v;       v.push_back( Foo::MEMBER );       // undefined reference to `Foo::MEMBER'     v.push_back( (int) Foo::MEMBER ); // OK       return 0; }
439
Consider:  namespace WindowsApplication1 {     public partial class Form1 : Form     {         public Form1()         {             InitializeComponent();         }          private void button1_Click(object sender, EventArgs e)         {             //int[] val = { 0, 0};             int val;             if (textBox1.Text == "")             {                 MessageBox.Show("Input any no");             }             else             {                 val = Convert.ToInt32(textBox1.Text);                 Thread ot1 = new Thread(new ParameterizedThreadStart(SumData));                 ot1.Start(val);             }         }          private static void ReadData(object state)         {             System.Windows.Forms.Application.Run();         }          void setTextboxText(int result)         {             if (this.InvokeRequired)             {                 this.Invoke(new IntDelegate(SetTextboxTextSafe), new object[] { result });             }             else             {                 SetTextboxTextSafe(result);             }         }          void SetTextboxTextSafe(int result)         {             label1.Text = result.ToString();         }          private static void SumData(object state)         {             int result;             //int[] icount = (int[])state;             int icount = (int)state;              for (int i = icount; i &gt; 0; i--)             {                 result += i;                 System.Threading.Thread.Sleep(1000);             }             setTextboxText(result);         }          delegate void IntDelegate(int result);          private void button2_Click(object sender, EventArgs e)         {             Application.Exit();         }     } }   Why is this error occurring?     An object reference is required for the nonstatic field, method, or property 'WindowsApplication1.Form1.setTextboxText(int)
22
Why does the order in which libraries are linked sometimes cause errors in GCC?
35333
The  is a fully paid up member of the 's , but it lately seems rather orphaned by the developer world. I still use quite a lot of singletons, especially for , and while you have to be a bit careful about multithreading issues (like any class actually), I fail to see why they are so awful.  Stack Overflow especially seems to assume that everyone agrees that Singletons are evil. Why?  Please support your answers with "facts, references, or specific expertise"