let's say I am trying to do something like this:  header.h:  template &lt;typename T&gt; class foo {  private:      // members... public:     foo();     ~foo(); }   source.cpp:  template &lt;typename T&gt; foo::foo() {     // do something...  }  template &lt;typename T&gt; foo::~foo() {     // do something...  }   but I keep getting error:   error LNK2019: unresolved external symbol ...   I am working on Microsoft Visual Studio 2015;
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.)
30528
I'd like to be able to introspect a C++ class for its name, contents (i.e. members and their types) etc. I'm talking native C++ here, not managed C++, which has reflection. I realise C++ supplies some limited information using RTTI. Which additional libraries (or other techniques) could supply this information?
18976
Is it possible to write a template that changes behavior depending on if a certain member function is defined on a class?  Here's a simple example of what I would want to write:  template&lt;class T&gt; std::string optionalToString(T* obj) {     if (FUNCTION_EXISTS(T-&gt;toString))         return obj-&gt;toString();     else         return "toString not defined"; }   So, if class T has toString() defined, then it uses it; otherwise, it doesn't. The magical part that I don't know how to do is the "FUNCTION_EXISTS" part.
14927
Disclaimer Yes, I am fully aware that what I am asking about is totally stupid and that anyone who would wish to try such a thing in production code should be fired and/or shot.  I'm mainly looking to see if can be done. Now that that's out of the way, is there any way to access private class members in C++ from outside the class?  For example, is there any way to do this with pointer offsets? (Naive and otherwise non-production-ready techniques welcome) Update As noted in the comments, I asked this question because I wanted to write a blog post on over-encapsulation (and how it affects TDD).  I wanted to see if there was a way to say &quot;using private variables isn't a 100% reliable way to enforce encapsulation, even in C++.&quot;  At the end, I decided to focus more on how to solve the problem rather than why it's a problem, so I didn't feature some of the stuff brought up here as prominently as I had planned, but I still left a link. At any rate, if anyone's interested in how it came out, here it is:   (I suggest reading it before you decide that I'm crazy).
7570
I noticed C++ will not compile the following:  class No_Good {   static double const d = 1.0; };   However it will happily allow a variation where the double is changed to an int, unsigned, or any integral type:  class Happy_Times {   static unsigned const u = 1; };   My solution was to alter it to read:  class Now_Good {   static double d() { return 1.0; } };   and figure that the compiler will be smart enough to inline where necessary... but it left me curious.  Why would the C++ designer(s) allow me to static const an int or unsigned, but not a double?  Edit: I am using visual studio 7.1 (.net 2003) on Windows XP.  Edit2:  Question has been answered, but for completion, the error I was seeing:  error C2864: 'd' : only const static integral data members can be initialized inside a class or struct
65196
This isn't a design question, really, though it may seem like it. (Well, okay, it's kind of a design question). What I'm wondering is why the C++ std::fstream classes don't take a std::string in their constructor or open methods. Everyone loves code examples so:  #include &lt;iostream&gt; #include &lt;fstream&gt; #include &lt;string&gt;  int main() {     std::string filename = "testfile";           std::ifstream fin;      fin.open(filename.c_str()); // Works just fine.     fin.close();      //fin.open(filename); // Error: no such method.     //fin.close(); }   This gets me all the time when working with files. Surely the C++ library would use std::string wherever possible?
38258
Most of my C/C++ development involves monolithic module files and absolutely no classes whatsoever, so usually when I need to make a DLL with accessible functions I just export them using the standard __declspec(dllexport) directive.  Then access them either dynamically via LoadLibrary() or at compile time with a header and lib file.   How do you do this when you want to export an entire class (and all it's public methods and properties)?  Is it possible to dynamically load that class at runtime and if so, how?  How would you do it with a header and lib for compile time linking?
46690
In XAML I can declare a DataTemplate so that the template is used whenever a specific type is displayed. For example, this DataTemplate will use a TextBlock to display the name of a customer:  &lt;DataTemplate DataType="{x:Type my:Customer}"&gt;     &lt;TextBlock Text="{Binding Name}" /&gt; &lt;/DataTemplate&gt;   I'm wondering if it's possible to define a DataTemplate that will be used any time an IList&lt;Customer&gt; is displayed. So if a ContentControl's Content is, say, an ObservableCollection&lt;Customer&gt; it would use that template.  Is it possible to declare a generic type like IList in XAML using the {x:Type} Markup Extension?
5983
The title is kind of obscure. What I want to know is if this is possible:  string typeName = &lt;read type name from somwhere&gt;; Type myType = Type.GetType(typeName);  MyGenericClass&lt;myType&gt; myGenericClass = new MyGenericClass&lt;myType&gt;();   Obviously, MyGenericClass is described as:  public class MyGenericClass&lt;T&gt;   Right now, the compiler complains that 'The type or namespace 'myType' could not be found." There has got to be a way to do this.
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'
64192
I have some stuff in settings.py that I'd like to be able to access from a template, but I can't figure out how to do it. I already tried  {{CONSTANT_NAME}}   but that doesn't seem to work. Is this possible?
29293
Say I have classes Foo and Bar set up like this:  class Foo { public:     int x;      virtual void printStuff()     {         std::cout &lt;&lt; x &lt;&lt; std::endl;     } };  class Bar : public Foo { public:     int y;      void printStuff()     {         // I would like to call Foo.printStuff() here...         std::cout &lt;&lt; y &lt;&lt; std::endl;     } };   As annotated in the code, I'd like to be able to call the base class's function that I'm overriding. In Java there's the super.funcname() syntax. Is this possible in C++?
39454
Why do we need both using namespace and include directives in C++ programs?  For example,   #include &lt;iostream&gt;  using namespace std;  int main() {  cout &lt;&lt; "Hello world"; }   Why is it not enough to just have #include &lt;iostream&gt; or just have using namespace std and get rid of the other?  (I am thinking of an analogy with Java, where import java.net.* will import everything from java.net, you don't need to do anything else.)
42083
What are the C++ coding and file organization guidelines you suggest for people who have to deal with lots of interdependent classes spread over several source and header files?  I have this situation in my project and solving class definition related errors crossing over several header files has become quite a headache.
56080
I have a method in an interface that I want to deprecate with portable C++. When I Googled for this all I got was a Microsoft specific solution;  and . A second prize solution would be to ifdef a MSVC and a GCC solution.
82685
I checked out a copy of a C++ application from SourceForge (HoboCopy, if you're curious) and tried to compile it.  Visual Studio tells me that it can't find a particular header file. I found the file in the source tree, but where do I need to put it, so that it will be found when compiling?    Are there special directories?
42167
The following code:  template &lt;typename S, typename T&gt; struct foo {    void bar(); };  template &lt;typename T&gt; void foo &lt;int, T&gt;::bar() { }   gives me the error  invalid use of incomplete type 'struct foo&lt;int, T&gt;' declaration of 'struct foo&lt;int, T&gt;'   (I'm using gcc.) Is my syntax for partial specialization wrong? Note that if I remove the second argument:  template &lt;typename S&gt; struct foo {    void bar(); };  template &lt;&gt; void foo &lt;int&gt;::bar() { }   then it compiles correctly.