I Have the following code in Student.h  #ifndef _student_h_ #define _student_h_  template  &lt;class T&gt; class Student { public:     Student();     ~Student();     void setWage(float hourlyWage);     void addHours(float hoursWorked);     bool pay();     bool writeCheck(float value);     float getTotalEarnings();     float getStudentCut();     float getSwauCut();     float getWage(); private:     float wage;     float hours;     float swauCut;     float studentCut;     float totalEarnings; }; #include "student.tpp" //Works with .tpp, errors with .cpp  #endif   As im trying to separate my code I attempted to place the following code into both a .cpp and a .tpp for testing.   #pragma once #include "stdafx.h" #include "Student.h"  template &lt;class T&gt; Student&lt;T&gt;::Student() {     wage = 0.0f;     hours = 0.0f;     swauCut = 0.0f;     studentCut = 0.0f;     totalEarnings = 0.0f; }  template &lt;class T&gt; Student&lt;T&gt;::~Student() { }  template &lt;class T&gt; void Student&lt;T&gt;::setWage(float hourlyWage) {     wage = hourlyWage; }  template &lt;class T&gt; void Student&lt;T&gt;::addHours(float hoursWorked) {     hours += hoursWorked; } template &lt;class T&gt; bool Student&lt;T&gt;::pay() {     if (hours == 0 || wage == 0)         return false;     studentCut += .25*(hours * wage);     swauCut += .75*(hours * wage);     totalEarnings += hours * wage;     hours = 0.0f;     return true; } template &lt;class T&gt; bool Student&lt;T&gt;::writeCheck(float value) {     if (value &lt; studentCut){         studentCut -= value;         return true;     }      return false; } template &lt;class T&gt; float Student&lt;T&gt;::getTotalEarnings() {     return totalEarnings; } template &lt;class T&gt; float Student&lt;T&gt;::getStudentCut() {     return studentCut; } template &lt;class T&gt; float Student&lt;T&gt;::getSwauCut() {     return swauCut; } template &lt;class T&gt; float Student&lt;T&gt;::getWage() {     return wage; }   My issue is that if I use the .cpp file and comment out the tpp file I get all sorts of errors. However if I just #include Student.tpp the files compile fine and work. I was under the impression that cpp and tpp were relatively the same?   The errors im getting are:  Error1  error C2995: 'Student&lt;T&gt;::Student(void)' : function template has already been defined   c:\users\aurelib.cs\desktop\studentproject\studentproject\student.cpp   13  1   StudentProject Error2  error C2995: 'Student&lt;T&gt;::~Student(void)' : function template has already been defined  c:\users\aurelib.cs\desktop\studentproject\studentproject\student.cpp   18  1   StudentProject   .... for all the functions.  If I remove the #include "Student.h" from the .cpp file I get Syntax errors.   I am using Visual Studios. And like I said I have no problem when I #include "Student.tpp" at the bottom of the template. But when I use the same code and #include "Student.cpp" instead.  Thanks so much 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.)
30163
What is the difference between this:  void MyMethod(IMyInterface value) {     //... }   and this:  void MyMethod&lt;T&gt;(T value) where T : IMyInterface {     //... }
113530
I want to make this specialized w/o changing main. Is it possible to specialize something based on its base class? I hope so.  -edit-  I'll have several classes that inherit from SomeTag. I don't want to write the same specialization for each of them.  class SomeTag {}; class InheritSomeTag : public SomeTag {};  template &lt;class T, class Tag=T&gt; struct MyClass { };  template &lt;class T&gt; struct MyClass&lt;T, SomeTag&gt; {     typedef int isSpecialized; };  int main() {     MyClass&lt;SomeTag&gt;::isSpecialized test1; //ok     MyClass&lt;InheritSomeTag&gt;::isSpecialized test2; //how do i make this specialized w/o changing main()     return 0; }
56384
When declaring a template, I am used to having this kind of code:  template &lt;class T&gt;   But , they used:  template &lt;unsigned int N&gt;   I checked that it compiles. But what does it mean? Is it a non-type parameter? And if so, how can we have a template without any type parameter?
60199
I am new to C++ and I had a few general questions about code separation. I have currently built a small application, all in one file.  What I want to do now is convert this into separate files such that they contain similar code or whatnot.  My real question right now is, how do I know how to separate things? What is the invisible margin that code should be separated at?    Also, what's the point of header files? Is it to forward declare methods and classes so I can use them in my code before they are included by the linker during compilation?    Any insight into methods or best practises would be great, thanks!
60879
Can anyone recommend any good resources for learning C++ Templates?  Many thanks.
92470
I'm wondering about instances when it makes sent to use #define and #if statements. I've known about it for a while, but never incorporated it into my way of coding. How exactly does this affect the compilation?     Is #define the only thing that determines if the code is included when compiled? If I have #define DEBUGme as a custom symbol, the only way to exclude it from compile is to remove this #define statement?
25980
suggested to have templates for tags. Their example was with the  tag on Stack Overflow.  I'm suggesting a more general approach:  Templates, Mark II  I suggest that each site will have a template. That template will prefill the  textbox when a new question is asked. However, it will be completely optional.  Something like (for Stack Overflow):  ## The Problem ## { Please tell us what the problem is with as many details as possible.   Include language, operating system, and any other relevant information.   Remember to add the relevant tags below, too. } ## Attempted Solutions ## { Please tell us what you've tried so far. Have you tried searching Google?    What queries? } ## Existing Code ## { Do you have any existing code to show? Please keep it short and simple.    We should be able to detect the problem right away. }   Why am I suggesting this?  Well, it seems as though most users will stick with the default. They don't bother to read, and so prefilling the question form for them with information that we know might be relevant, will forceWe'll get to that in a second them to actually stop and read.  Scenarios:   Best case: User arrives to ask a question, actually reads the template, fills the correct information in the placeholders. (Yeah, because that'll happen a lot) Neutral: User simply removes all of the template text and rewrites their own question. Result is exact same as we have currently. Worst case: User will ignore or misunderstand the template, will use it wrong or not at all and we'll get something like  &lt;template text here, still unfilled&gt; &lt;(crappy) question&gt;   This can be solved in a couple different ways. We could detect the template tags (i.e. { }, subject to discussion), and throw an error when a user attempts to post with them still available. We could also act more autonomously and remove placeholders which still exist.    Some more points:   Templates should be per-site, not per-tag. Sure, there are differences between the tags, but all questions are expected to have the same general information, and this isn't meant to be a rule, but a guideline to help write better questions. Site templates should be editable by that site's mods, like close reasons. We could autofocus on the first placeholder, and perhaps implement TAB to jump from one placeholder to the other, similar to mixins in IDEs. This doesn't have to be implemented everywhere. This suggestion makes sense to Stack Overflow or Superuser, but not to Anime &amp; Manga or Gaming All of the details are subject to discussion, and all discussion is welcomed.
8886
I've seen this done in Borland's  environment, but I'm not sure how to go about it for a C# application I'm working on. Are there best practices or gotchas to look out for?
124139
I have a class which has many small functions. By small functions, I mean functions that doesn't do any processing but just return a literal value. Something like:  string Foo::method() const{     return "A"; }   I have created a header file "Foo.h" and source file "Foo.cpp". But since the function is very small, I am thinking about putting it in the header file itself. I have the following questions:   Is there any performance or other issues if I put these function definition in header file? I will have many functions like this. My understanding is when the compilation is done, compiler will expand the header file and place it where it is included. Is that correct?
48897
I have a C++ library that provides various classes for managing data.  I have the source code for the library.  I want to extend the C++ API to support C function calls so that the library can be used with C code and C++ code at the same time.  I'm using GNU tool chain (gcc, glibc, etc), so language and architecture support are not an issue.  Are there any reasons why this is technically not possible?  Are there any gotcha's that I need to watch out for?  Are there resources, example code and/or documentation available regarding this?    Some other things that I have found out:   Use the following to wrap your C++ headers that need to be used by C code.     #ifdef __cplusplus extern "C" {   #endif   //   // Code goes here ...   //   #ifdef __cplusplus   } // extern "C"   #endif    Keep "real" C++ interfaces in separate header files that are not included by C.  Think  here.  Using #ifndef __cplusplus #error stuff helps here to detect any craziness. Careful of C++ identifiers as names in C code Enums varying in size between C and C++ compilers.  Probably not an issue if you're using GNU tool chain, but still, be careful. For structs follow the following form so that C does not get confused.  typedef struct X { ... } X  Then use pointers for passing around C++ objects, they just have to be declared in C as struct X where X is the C++ object.   All of this is courtesy of a friend who's a wizard at C++.
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.
70300
What is the difference between using #include&lt;filename&gt; and #include&lt;filename.h> in ? Which of the two is used and why is it is used?
8017
I often find myself in a situation where I am facing multiple compilation/linker errors in a C++ project due to some bad design decisions (made by someone else :) ) which lead to circular dependencies between C++ classes in different header files (can happen also in the same file). But fortunately(?) this doesn't happen often enough for me to remember the solution to this problem for the next time it happens again.  So for the purposes of easy recall in the future I am going to post a representative problem and a solution along with it. Better solutions are of-course welcome.     A.h  class B; class A {     int _val;     B *_b; public:      A(int val)         :_val(val)     {     }      void SetB(B *b)     {         _b = b;         _b-&gt;Print(); // COMPILER ERROR: C2027: use of undefined type 'B'     }      void Print()     {         cout&lt;&lt;"Type:A val="&lt;&lt;_val&lt;&lt;endl;     } };       B.h  #include "A.h" class B {     double _val;     A* _a; public:      B(double val)         :_val(val)     {     }      void SetA(A *a)     {         _a = a;         _a-&gt;Print();     }      void Print()     {         cout&lt;&lt;"Type:B val="&lt;&lt;_val&lt;&lt;endl;     } };       main.cpp  #include "B.h" #include &lt;iostream&gt;  int main(int argc, char* argv[]) {     A a(10);     B b(3.14);     a.Print();     a.SetB(&amp;b);     b.Print();     b.SetA(&amp;a);     return 0; }
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?
91306
I hope someone is able to help me. The problem is the following one: 1)I have module in which a assign the variable in order to send to the template file php.tpl  &lt;?php ... $testvar="Hello from alex!"; $variables['testvar'] = $testvar;  ... ?&gt;   This variable can be shown in the php.tpl file like   &lt;?php print $testvar?&gt;   2)I have separated .js file How can i get access to this varuiable inside .js file?  I know how it'll be if .js file is inside .php.tpl:  &lt;?php $testvar="Hello from alex!"; ?&gt; &lt;script type="text/javascript"&gt; var myVar = '&lt;?php print $testvar?&gt;'; &lt;/script&gt;   The problem is how to do the same if these two files .js and .php.tpl are separated?
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.