Some "undefined reference error" questions are already posted on StackOverflow but I couldn't find solution to my problem.  My project contains 5 files: 5.cpp vector_base.h vector_base.cpp vector.h vector.cpp  I compile it with:  g++ -std=c++11 vector_base.cpp vector.cpp 5.cpp -o 5   And get the following error:  /tmp/ccO8NeGJ.o: In function `main': 5.cpp:(.text.startup+0x21): undefined reference to `Vector&lt;int, std::allocator&lt;int&gt; &gt;::Vector(unsigned int, int const&amp;, std::allocator&lt;int&gt; const&amp;)' 5.cpp:(.text.startup+0x2b): undefined reference to `Vector&lt;int, std::allocator&lt;int&gt; &gt;::destroy_elements()' collect2: error: ld returned 1 exit status   well, code looks fine for me. Where did I mess up?  The code is as follows:   5.cpp  #include "vector.h"  int main() {     Vector&lt;int&gt; a{10, 0}; }   vector.h  #ifndef VECTOR_H #define VECTOR_H  #include "vector_base.h"  template&lt;class T, class A = std::allocator&lt;T&gt;&gt; class Vector { private:     vector_base&lt;T,A&gt; vb;     void destroy_elements();  public:     using size_type = unsigned int;      explicit Vector(size_type n, const T&amp; val = T(), const A&amp; a = A());     ~Vector() { destroy_elements(); } };  #endif //VECTOR_H   vector.cpp  #include "vector.h"  #include &lt;algorithm&gt; #include &lt;memory&gt;  template&lt;class T, class A&gt; Vector&lt;T,A&gt;::Vector(size_type n, const T&amp; val, const A&amp; a)     :vb{a, n} {     std::uninitialized_fill(vb.elem, vb.elem + n, val); }  template&lt;class T, class A&gt; void Vector&lt;T,A&gt;::destroy_elements() {     for(T* p = vb.elem; p!=vb.space; ++p){         p-&gt;~T();     }     vb.space=vb.elem; }   vector_base.h  #ifndef VECTOR_BASE_H #define VECTOR_BASE_H  #include &lt;memory&gt;  template&lt;class T, class A = std::allocator&lt;T&gt;&gt; struct vector_base {      T* elem;     T* space;     T* last;     A alloc;      using size_type = unsigned int;      vector_base(const A&amp; a, typename A::size_type n)         :alloc{a}, elem{alloc.allocate(n)}, space{elem+n}, last{elem+n} {}      ~vector_base() { alloc.deallocate(elem, last-elem); } }; #endif //VECTOR_BASE_H   vector_base.cpp  #include "vector_base.h" #include &lt;algorithm&gt;  template&lt;class T, class A&gt; vector_base&lt;T,A&gt;::vector_base(const vector_base&amp;&amp; a)     :alloc{a.alloc}, elem{a.elem}, space{a.space}, last{a.last} {     a.elem = a.space = a.last = nullptr; }  template&lt;class T, class A&gt; vector_base&lt;T,A&gt;&amp; vector_base&lt;T,A&gt;::operator=(const vector_base&amp;&amp; a) {     swap(*this, a);     return *this; }   I tried compile every cpp file separately and then link all but it didn't work also.
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.)
35055
It would be very useful to be able to overload the . operator in C++ and return a reference to an object.  You can overload operator-&gt; and operator* but not operator.  Is there a technical reason for this?
42535
If I have a class as follows     class Example_Class     {        private:          int x;           int y;         public:           Example_Class()           {               x = 8;              y = 9;          }        ~Example_Class()         { }     };   And a struct as follows  struct {    int x;    int y; } example_struct;   Is the structure in memory of the  example_struct simmilar to that in Example_Class  for example if I do the following  struct example_struct foo_struct; Example_Class foo_class = Example_Class();  memcpy(&amp;foo_struct, &amp;foo_class, sizeof(foo_struct));   will foo_struct.x = 8 and foo_struct.y = 9 (ie: the same values as the x,y values in the foo_class) ?  The reason I ask is I have a C++ library (don't want to change it) that is sharing an object with C code and I want to use a struct to represent the object coming from the C++ library. I'm only interested in the attributes of the object.  I know the ideal situation would be to have Example_class wrap arround a common structure between the C and C++ code but it is not going to be easy to change the C++ library in use.
73550
Quick question: what is the compiler flag to allow g++ to spawn multiple instances of itself in order to compile large projects quicker (for example 4 source files at a time for a multi-core CPU)?
8198
I am writing a small matrix library in C++ for matrix operations. However my compiler complains, where before it did not. This code was left on a shelf for 6 months and in between I upgraded my computer from debian etch to lenny (g++ (Debian 4.3.2-1.1) 4.3.2 ) however I have the same problem on a Ubuntu system with the same g++.  Here is the relevant part of my matrix class:  namespace Math {     class Matrix     {     public:          [...]          friend std::ostream&amp; operator&lt;&lt; (std::ostream&amp; stream, const Matrix&amp; matrix);     } }   And the "implementation":  using namespace Math;  std::ostream&amp; Matrix::operator &lt;&lt;(std::ostream&amp; stream, const Matrix&amp; matrix) {      [...]  }   This is the error given by the compiler:     matrix.cpp:459: error: 'std::ostream&amp;   Math::Matrix::operator&lt;&lt;(std::ostream&amp;,   const Math::Matrix&amp;)' must take   exactly one argument   I'm a bit confused by this error, but then again my C++ has gotten a bit rusty after doing lots of Java those 6 months. :-)
60675
As a general rule, I prefer using value rather than pointer semantics in C++ (ie using vector&lt;Class&gt; instead of vector&lt;Class*&gt;). Usually the slight loss in performance is more than made up for by not having to remember to delete dynamically allocated objects.  Unfortunately, value collections don't work when you want to store a variety of object types that all derive from a common base. See the example below.  #include &lt;iostream&gt;  using namespace std;  class Parent {     public:         Parent() : parent_mem(1) {}         virtual void write() { cout &lt;&lt; "Parent: " &lt;&lt; parent_mem &lt;&lt; endl; }         int parent_mem; };  class Child : public Parent {     public:         Child() : child_mem(2) { parent_mem = 2; }         void write() { cout &lt;&lt; "Child: " &lt;&lt; parent_mem &lt;&lt; ", " &lt;&lt; child_mem &lt;&lt; endl; }          int child_mem; };  int main(int, char**) {     // I can have a polymorphic container with pointer semantics     vector&lt;Parent*&gt; pointerVec;      pointerVec.push_back(new Parent());     pointerVec.push_back(new Child());      pointerVec[0]-&gt;write();      pointerVec[1]-&gt;write();       // Output:     //     // Parent: 1     // Child: 2, 2      // But I can't do it with value semantics      vector&lt;Parent&gt; valueVec;      valueVec.push_back(Parent());     valueVec.push_back(Child());    // gets turned into a Parent object :(      valueVec[0].write();         valueVec[1].write();          // Output:     //      // Parent: 1     // Parent: 2  }   My question is: Can I have have my cake (value semantics) and eat it too (polymorphic containers)? Or do I have to use pointers?
71767
I could be totally wrong here, but as I understand it, C++ doesn't really have a native "pointer to member function" type.  I know you can do tricks with Boost and mem_fun etc. But why did the designers of C++ decide not to have a 64-bit pointer containing a pointer to the function and a pointer to the object, for example?  What I mean specifically is a pointer to a member function of a particular object of unknown type.  I.E. something you can use for a callback.  This would be a type which contains two values. The first value being a pointer to the function, and the second value being a pointer to the specific instance of the object.  What I do not mean is a pointer to a general member function of a class. E.G.  int (Fred::*)(char,float)   It would have been so useful and made my life easier.  Hugo
68932
I am using the RSA Algorithm for encryption/decryption, and in order to decrypt the files you have to deal with some pretty big values. More specifically, things like   P = C^d % n   = 62^65 % 133   Now that is really the only calculations that ill be doing. I have tried using Matt McCutchen's BigInteger Library, but I am getting a lot of compiler errors during linking, such as:  encryption.o(.text+0x187):encryption.cpp: undefined reference to `BigInteger::BigInteger(int)'  encryption.o(.text+0x302):encryption.cpp: undefined reference to `operator&lt;&lt;(std::ostream&amp;, BigInteger const&amp;)'  encryption.o(.text$_ZNK10BigIntegermlERKS_[BigInteger::operator*(BigInteger const&amp;) const]+0x63):encryption.cpp: undefined reference to `BigInteger::multiply(BigInteger const&amp;, BigInteger const&amp;)'   So I was wondering what would be the best way to go about handling the really big integers that come out of the RSA Algorithm.  I heard that a possibility would be to declare your variables as a double long, so...  long long decryptedCharacter;   but I'm not sure exactly how big of an integer that can store.    Well for example, I try to compile and run the following program using dev C++:  #include iostream  #include "bigint\BigIntegerLibrary.hh"  using namespace std;  int main() {     BigInteger a = 65536;     cout &lt;&lt; (a * a * a * a * a * a * a * a);     return 0; }   then I get those errors.  Derek, I thought that by including the BigIntegerLibrary.hh file, that the compiler would go through and compile all the necessary files that it will use.  How should I try and compile the program above in order to resolve the linking errors?
5040
I'm using an API that requires me to pass a function pointer as a callback. I'm trying to use this API from my class but I'm getting compilation errors.   Here is what I did from my constructor:  m_cRedundencyManager-&gt;Init(this-&gt;RedundencyManagerCallBack);   This doesn't compile - I get the following error:      Error  8 error C3867: 'CLoggersInfra::RedundencyManagerCallBack': function call missing argument list; use '&amp;CLoggersInfra::RedundencyManagerCallBack' to create a pointer to member   I tried the suggestion to use &amp;CLoggersInfra::RedundencyManagerCallBack - didn't work for me.   Any suggestions/explanation for this??  I'm using VS2008.  Thanks!!
7923
template&lt;typename T, size_t n&gt; size_t array_size(const T (&amp;)[n]) {     return n; }   The part that I don't get is the parameters for this template function. What happens with the array when I pass it through there that gives n as the number of elements in the array?
47351
Here is a little test program:   #include &lt;iostream&gt;  class Test { public:     static void DoCrash(){ std::cout&lt;&lt; "TEST IT!"&lt;&lt; std::endl; } };  int main() {     Test k;     k.DoCrash(); // calling a static method like a member method...      std::system("pause");      return 0; }   On VS2008 + SP1 (vc9) it compiles fine: the console just display "TEST IT!".  As far as I know, static member methods shouldn't be called on instanced object.   Am I wrong? Is this code correct from the standard point of view? If it's correct, why is that? I can't find why it would be allowed, or maybe it's to help using "static or not" method in templates?
21478
I have a c++ header file containing a class. I want to use this class in several projects, bu I don't want to create a separate library for it, so I'm putting both methods declarations and definitions in the header file:     // example.h #ifndef EXAMPLE_H_ #define EXAMPLE_H_ namespace test_ns{  class TestClass{ public:     void testMethod(); };  void TestClass::testMethod(){     // some code here... }  } // end namespace test_ns #endif   If inside the same project I include this header from more than one cpp file, I get an error saying "multiple definition of test_ns::TestClass::testMethod()", while if I put the method definition inside the class body this does not happen:  // example.h #ifndef EXAMPLE_H_ #define EXAMPLE_H_ namespace test_ns{  class TestClass{ public:     void testMethod(){         // some code here...     } };  } // end namespace test_ns #endif   Since the class is defined inside a namespace, shouldn't the two forms be equivalent? Why is the method considered to be defined twice in the first case?
107235
#include&lt;iostream&gt; using namespace std;  class A  {    int a;    int b;    public:    void eat()    {       cout&lt;&lt;"A::eat()"&lt;&lt;endl;    } };  class B: public A {    public:    void eat()    {        cout&lt;&lt;"B::eat()"&lt;&lt;endl;     }  };  class C: public A {     public:    void eat()     {        cout&lt;&lt;"C::eat()"&lt;&lt;endl;     }  };  class D: public B, C {  };  int foo(A *ptr) {  ptr-&gt;eat();  } main() {  D obj; foo(&amp;(obj.B)); //error. How do i call with D's B part.  }   The above foo call is a compile time error.  I want to call foo with obj's B part without using virtual inheritance. How do i do that.   Also, in case of virtual inheritance, why the offset information need to be stored in the vtable. This can be determined at the compile time itself. In the above case, if we pass foo with D's object, at compile time only we can calculate the offset of D's A part.
21491
Why does C++ have header files and .cpp files?
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?
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.
15725
I've been getting this undefined symbol building with this command line:  $ gcc test.cpp Undefined symbols:   "___gxx_personality_v0", referenced from:   etc...   test.cpp is simple and should build fine.  What is the deal?