I have no idea why this is happenning, since I think I have everything properly declared and defined.  I have the following program, designed with templates. It's a simple implementation of a queue, with the member functions "add", "substract" and "print".  I have defined the node for the queue in the fine "nodo_colaypila.h":  #ifndef NODO_COLAYPILA_H #define NODO_COLAYPILA_H  #include &lt;iostream&gt;  template &lt;class T&gt; class cola;  template &lt;class T&gt; class nodo_colaypila {         T elem;         nodo_colaypila&lt;T&gt;* sig;         friend class cola&lt;T&gt;;     public:         nodo_colaypila(T, nodo_colaypila&lt;T&gt;*);  };   Then the implementation in "nodo_colaypila.cpp"  #include "nodo_colaypila.h" #include &lt;iostream&gt;  template &lt;class T&gt; nodo_colaypila&lt;T&gt;::nodo_colaypila(T a, nodo_colaypila&lt;T&gt;* siguiente = NULL) {     elem = a;     sig = siguiente;//ctor }   Afterwards, the definition and declaration of the queue template class and its functions:  "cola.h":  #ifndef COLA_H #define COLA_H  #include "nodo_colaypila.h"  template &lt;class T&gt; class cola {         nodo_colaypila&lt;T&gt;* ult, pri;     public:         cola&lt;T&gt;();         void anade(T&amp;);         T saca();         void print() const;         virtual ~cola();  };   #endif // COLA_H   "cola.cpp":  #include "cola.h" #include "nodo_colaypila.h"  #include &lt;iostream&gt;  using namespace std;  template &lt;class T&gt; cola&lt;T&gt;::cola() {     pri = NULL;     ult = NULL;//ctor }  template &lt;class T&gt; void cola&lt;T&gt;::anade(T&amp; valor) {     nodo_colaypila &lt;T&gt; * nuevo;      if (ult)     {         nuevo = new nodo_colaypila&lt;T&gt; (valor);         ult-&gt;sig = nuevo;         ult = nuevo;     }     if (!pri)     {         pri = nuevo;     } }  template &lt;class T&gt; T cola&lt;T&gt;::saca() {     nodo_colaypila &lt;T&gt; * aux;     T valor;      aux = pri;     if (!aux)     {         return 0;     }     pri = aux-&gt;sig;     valor = aux-&gt;elem;     delete aux;     if(!pri)     {         ult = NULL;     }     return valor; }  template &lt;class T&gt; cola&lt;T&gt;::~cola() {     while(pri)     {         saca();     }//dtor }  template &lt;class T&gt; void cola&lt;T&gt;::print() const {     nodo_colaypila &lt;T&gt; * aux;     aux = pri;     while(aux)     {         cout &lt;&lt; aux-&gt;elem &lt;&lt; endl;         aux = aux-&gt;sig;     } }   Then, I have a program to test these functions as follows:  "main.cpp"  #include &lt;iostream&gt; #include "cola.h" #include "nodo_colaypila.h"  using namespace std;  int main() {     float a, b, c;     string d, e, f;     cola&lt;float&gt; flo;     cola&lt;string&gt; str;      a = 3.14;     b = 2.71;     c = 6.02;     flo.anade(a);     flo.anade(b);     flo.anade(c);     flo.print();     cout &lt;&lt; endl;      d = "John";     e = "Mark";     f = "Matthew";     str.anade(d);     str.anade(e);     str.anade(f);     cout &lt;&lt; endl;      c = flo.saca();     cout &lt;&lt; "First In First Out Float: " &lt;&lt; c &lt;&lt; endl;     cout &lt;&lt; endl;      f = str.saca();     cout &lt;&lt; "First In First Out String: " &lt;&lt; f &lt;&lt; endl;     cout &lt;&lt; endl;      flo.print();     cout &lt;&lt; endl;     str.print();      cout &lt;&lt; "Hello world!" &lt;&lt; endl;     return 0; }   But when I build, the compiler throws errors in every instance of the template class:   undefined reference to `cola(float)::cola()'... (it's actually cola'&lt;'float'>'::cola(), but this doesn't let me use it like that.)  And so on. Altogether, 17 warnings, counting the ones for the member functions being called in the program.  Why is this? Those functions and constructors WERE defined. I thought that the compiler could replace the "T" in the template with "float", "string" or whatever; that was the advantage of using templates.  I read somewhere here that I should put the declaration of each function in the header file for some reason. Is that right? And if so, why?  Thanks in advance.
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.)
81239
I'm a C++ developer - not a java developer, but have to get this code working...  I have 2 public classes that will be used by another product.  I used the package directive in each of the java files.    package com.company.thing;  class MyClass ...   When I try to compile a test app that uses that I add   import com.company.thing.*;   The javac compiler fails with errors about com.company does not exist.  (even if I compile it in the same directory as the class files I just made a package of)  I am sure I am doing something bone-headed and silly.  I've read the  pages and tried to set up a directory structure like /com/company/thing  etc, but either I have totally screwed it all up or am missing something else.  EDIT thanks for the suggestions - I had tried the classpath previously.  It does not help.  I tried compiling   javac -classpath &lt;parent&gt; client.java    and the result is:  package com.company does not exist   I have the code I want to import (the two java files) in \com\company\product.  I compile those fine.  (they contain MyClass) I even made a jar file for them.  I copied the jar file up to the parent directory.  I then did (in the parent directory with the client java file)   javac -cp &lt;jarfile&gt; *.java   the result is:  cannot access MyClass bad class file: MyClass.class(:MyClass.class) class file contains wrong class: com.company.product.MyClass Please remove or make sure it appears in the correct subdirectory of the classpath.   EDIT  I got the client code to compile and run if I used the fully qualified name for MyClass and compiled it in the parent directory.  I am totally confused now.    compiled with no classpath set - just   javac *.java    in the parent directory - and it worked fine.  I can get a test app to compile, but that is not going to cut it when i have to integrate it into the production code.  Still looking for help.  EDIT:  Finally - not sure why it didn't work before - but I cleaned up all the files all over the directory structure and now it works.    Thanks
30655
This is the code in my template.php file.  function studiga_theme_theme($existing, $type, $theme, $path) {         $items['add_property_form'] = array(         'render element' =&gt; 'form',         'template' =&gt; 'node--14',         'path' =&gt; drupal_get_path('theme', 'studiga_theme') . '/templates/node',     );      return $items; }    function custom_add_property_form($form, &amp;$form_state) {     $form['first_name'] = array(         '#type' =&gt; 'textfield',         '#attributes' =&gt; array('placeholder' =&gt; t('First name')),     );     $form['last_name'] = array(         '#type' =&gt; 'textfield',         '#attributes' =&gt; array('placeholder' =&gt; t('Last name')),     );     $form['address'] = array(         '#type' =&gt; 'textfield',         '#attributes' =&gt; array('placeholder' =&gt; t('Address')),     );     $form['city'] = array(         '#type' =&gt; 'textfield',         '#attributes' =&gt; array('placeholder' =&gt; t('City')),     );     $form['state'] = array(         '#type' =&gt; 'select',         '#options' =&gt; array(             'default' =&gt; 'State',             '...' =&gt; '...',         ),     );     $form['zip'] = array(         '#type' =&gt; 'textfield',         '#attributes' =&gt; array('placeholder' =&gt; t('Zip')),     );     $form['email'] = array(         '#type' =&gt; 'textfield',         '#attributes' =&gt; array('placeholder' =&gt; t('Email')),     );     $form['phone'] = array(         '#type' =&gt; 'textfield',         '#attributes' =&gt; array('placeholder' =&gt; t('Phone')),     );     $form['submit'] = array(         '#type' =&gt; 'submit',         '#value' =&gt; 'Submit',     );      return $form; }   This is the code in node--14.tpl.php.                                          Contact Information                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  &lt;div class="row"&gt;     &lt;div class="small-12 medium-12 large-8 large-offset-2 columns"&gt;         &lt;?php print render($form['submit']); ?&gt;     &lt;/div&gt; &lt;/div&gt;     But no fields are appearing - what am I doing wrong? You can't have a straight forward  in a tpl.php file can you?
107586
Can a class overload methods that also exist in the publicly inherited interface? It seems like this is unambiguous and useful, but compilers (VC, Intel, GCC) all complain, at least by my construction. Below is a toy example. The inherited rebound() function has two clear overloads, yet this will not compile.  If you rename the rebound() method in either class, it works fine, but if they share the same member function name (even though they're overloaded with different argument types!) you get a fatal error of "too few arguments to function call."  The workaround is trivial (I'll just rename the methods) but I'm just trying to understand if this is a C++ restriction (and why it would be).   #include  class Bound { public:   Bound() : x0(0.0), x1(0.0) {};   Bound(double x) : x0(x), x1(x) {};   double width() const {return x1-x0;}   void rebound(const Bound *a, const Bound *b); private:   double x0, x1; };  void Bound::rebound(const Bound *a, const Bound *b) {   if (a && b) {     x0=std::min(a->x0, b->x0);     x1=std::max(a->x1, b->x1);   } }  class Node : public Bound { public:   Node(double x) : Bound(x), left(0), right(0) {};   Node(Node *a, Node *b) : left(a), right(b) {rebound();}   void rebound() { rebound(left, right); } private:   Node *left;   Node *right; };   int main() {   Node A(1.0);   Node B(2.0);   Node C(&A, &B); }
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!!
46642
Let's say I have a class that has a member called data which is a list.    I want to be able to initialize the class with, for example, a filename (which contains data to initialize the list) or with an actual list.  What's your technique for doing this?  Do you just check the type by looking at __class__?  Is there some trick I might be missing?  I'm used to C++ where overloading by argument type is easy.
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?
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?
55810
Several times, the  package uses \newtoks which (according to  website) means that a token register is allocated. An example code snippet is  \@ifdefinable\FR@everyfloat{\newtoks\FR@everyfloat} \FR@everyfloat={\let\@footnotetext\@mpfootnotetext   \def\@mpfn{mpfootnote}\def\thempfn{\thempfootnote}\c@mpfootnote\z@   \floatobjectset\floatfont}   Suppose I want to add something (e.g., \setlength{\footnotesep}{0.2\baselineskip}) to the definition of \FR@everyfloat. One way I successfully applied in  answer is to overwrite the original definition, i.e., write  \FR@everyfloat={\let\@footnotetext\@mpfootnotetext   \def\@mpfn{mpfootnote}\def\thempfn{\thempfootnote}\c@mpfootnote\z@   \floatobjectset\floatfont   \setlength{\footnotesep}{0.2\baselineskip}}   However, I'd like to avoid duplicating the whole token list content; instead, I want to append material to the list the same way one would use \g@addto@macro (or \appto from the  package). However, my naïve approach to write  \g@addto@macro{\FR@everyfloat}{\setlength{\footnotesep}{0.2\baselineskip}}   doesn't work -- obviously, token lists must be treated different from macros.  So: How does one append material to a token list?
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?
103430
My personal style with C++ has always to put class declarations in an include file, and definitions in a .cpp file, very much like stipulated in . Admittedly, part of the reason I like this style probably has to do with all the years I spent coding Modula-2 and Ada, both of which have a similar scheme with specification files and body files. I have a coworker, much more knowledgeable in C++ than I, who is insisting that all C++ declarations should, where possible, include the definitions right there in the header file. He's not saying this is a valid alternate style, or even a slightly better style, but rather this is the new universally-accepted style that everyone is now using for C++. I'm not as limber as I used to be, so I'm not really anxious to scrabble up onto this bandwagon of his until I see a few more people up there with him. So how common is this idiom really? Just to give some structure to the answers: Is it now The Way™, very common, somewhat common, uncommon, or bug-out crazy?
30163
What is the difference between this:  void MyMethod(IMyInterface value) {     //... }   and this:  void MyMethod&lt;T&gt;(T value) where T : IMyInterface {     //... }
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?
24839
class Outer {      class Inner {     public:         Inner() {}         void func() ;     };  private:     static const char* const MYCONST;     int var; };  void Outer::Inner::func() {     var = 1; }  const char* const Outer::MYCONST = "myconst";   This errors out when I compile with class Outer::Inner' has no member named `var'
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. :-)
122587
I am trying to find a generic way of accessing a set of containers. I have a standard vector and list in addition to another custom list.  The custom list defines an iterator;  class Iterator: public std::iterator&lt;std::forward_iterator_tag, T&gt; {     // ... }  Iterator begin() {     return (Iterator(root)); }  Iterator end() {     return (Iterator(NULL)); }   with the appropriate operators overloaded.   Ideally, I would like to do this;  class Foo { public:     Foo() {         std::list&lt;int&gt; x;         std::vector&lt;int&gt; y;         custom_list&lt;int&gt; z;          iter = x.begin(); // OR         iter = y.begin(); // OR         iter = z.begin();          // ...     }; private:     std::iterator&lt;int&gt; iter; };   But obviously these are all iterators of different types. I can assume all the containers are of the same type however.  Is there an elegant way to solve this problem?
47824
I've seen some examples of C++ using template template parameters (that is templates which take templates as parameters) to do policy-based class design. What other uses does this technique have?