int main()  {     Pair&lt;int, int&gt; A = makePair (10, 20);     cout &lt;&lt; A ;  return 0; }   it give me error here (makePair is not define) and i define it in header file  template&lt;class T1, class T2&gt; class Pair{ private:     T1 first;     T2 second; public:     Pair(T1, T2);     Pair() {};     Pair&lt;T1, T2&gt;  makePair(T1  a, T2  b);     void operator==(const Pair&amp; other);     friend ostream&amp; operator&lt;&lt;(ostream&amp; out, const Pair&lt;T1, T2&gt;&amp; A); };   and this is another file called pair.cpp that i used a template classes in it  template&lt;class T1, class T2&gt; Pair&lt;T1, T2&gt; makePair(T1   a, T2   b) {     return (Pair&lt;T1, T2&gt;(a, b)); }
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.)
18652
I have the following class in C++:  class a {     const int b[2];     // other stuff follows      // and here's the constructor     a(void); }   The question is, how do I initialize b in the initialization list, given that I can't initialize it inside the body of the function of the constructor, because b is const?  This doesn't work:  a::a(void) :      b([2,3]) {      // other initialization stuff }   Edit:  The case in point is when I can have different values for b for different instances, but the values are known to be constant for the lifetime of the instance.
81821
In the  library, they are overloading the () operator.  Is this a convention in C++? For callbacks, etc.?  I have seen this in code of a co-worker (who happens to be a big Boost fan). Of all the Boost goodness out there, this has only led to confusion for me.  Any insight as to the reason for this overload?
19841
I've been programming in C++ for a few years, and I've used STL quite a bit and have created my own template classes a few times to see how it's done.  Now I'm trying to integrate templates deeper into my OO design, and a nagging thought keeps coming back to me: They're just a macros, really... You could implement (rather UGLY) auto_ptrs using #defines, if you really wanted to.  This way of thinking about templates helps me understand how my code will actually work, but I feel that I must be missing the point somehow. Macros are meant evil incarnate, yet "template metaprogramming" is all the rage.  So, what ARE the real distinctions? and how can templates avoid the dangers that #define leads you into, like   Inscrutable compiler errors in places where you don't expect them? Code bloat?  Difficulty in tracing code? Setting Debugger Breakpoints?
37844
Is there a good reason why there is no Pair&lt;L,R&gt; in Java? What would be the equivalent of this C++ construct? I would rather avoid reimplementing my own.  It seems that 1.6 is providing something similar (AbstractMap.SimpleEntry&lt;K,V&gt;), but this looks quite convoluted.
15868
Why does this code:  class A {     public:          explicit A(int x) {} };  class B: public A { };  int main(void) {     B *b = new B(5);     delete b; }   Result in these errors:   main.cpp: In function ‘int main()’: main.cpp:13: error: no matching function for call to ‘B::B(int)’ main.cpp:8: note: candidates are: B::B() main.cpp:8: note:                 B::B(const B&)   Shouldn't B inherit A's constructor?  (this is using gcc)
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
69483
I have code like this:  template &lt;typename T, typename U&gt; struct MyStruct {     T aType;     U anotherType; };  class IWantToBeFriendsWithMyStruct {     friend struct MyStruct; //what is the correct syntax here ? };   What is the correct syntax to give friendship to the template ?
67728
Is there any way of doing parallel assignment in C++? Currently, the below compiles (with warnings)  #include &lt;iostream&gt;   int main() {    int a = 4;   int b = 5;   a, b = b, a;   std::cout &lt;&lt; "a: " &lt;&lt; a &lt;&lt; endl             &lt;&lt; "b: " &lt;&lt; b &lt;&lt; endl;    return 0; }   and prints:  a: 4 b: 5   What I'd like it to print ... if it weren't obvious, is:  a: 5 b: 4   As in, say, ruby, or python.
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?
29017
I'm asking for a template trick to detect if a class has a specific member function of a given signature.  The problem is similar to the one cited here  but not the same: in the item of Sutter's book he answered to the question that a class C MUST PROVIDE  a member function with a particular signature, else the program won't compile. In my problem I need to do something if a class has that function, else do "something else".   A similar problem was faced by boost::serialization but I don't like the solution they adopted: a template function that invokes by default a free function (that you have to define) with a particular signature unless you define a particular member function (in their case "serialize" that takes 2 parameters of a given type) with a particular signature, else a compile error will happens. That is to implement both intrusive and non-intrusive serialization.   I don't like that solution for two reasons:   To be non intrusive you must override the global "serialize" function that is in boost::serialization namespace, so you have IN YOUR CLIENT CODE to open namespace boost and namespace serialization! The stack to resolve that mess was 10 to 12 function invocations.   I need to define a custom behavior for classes that has not that member function, and my entities are inside different namespaces (and I don't want to override a global function defined in one namespace while I'm in another one)  Can you give me a hint to solve this puzzle?
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.)
80591
Although I'm doubtful, I'm curious as to whether it's possible to extract primitive-type template parameters from an existing type, perhaps using RTTI.  For example:  typedef std::bitset&lt;16&gt; WordSet;   Would it be possible to extract the number 16 in the above code without hard-coding it elsewhere? Compiler specific implementations are welcome, though I'm particularly interested in g++.
8090
I have a file: Base.h  class Base; class DerivedA : public Base; class DerivedB : public Base;  /*etc...*/   and another file: BaseFactory.h  #include "Base.h"  class BaseFactory { public:   BaseFactory(const string &amp;sClassName){msClassName = sClassName;};    Base * Create()   {     if(msClassName == "DerivedA")     {       return new DerivedA();     }     else if(msClassName == "DerivedB")     {       return new DerivedB();     }     else if(/*etc...*/)     {       /*etc...*/     }   }; private:   string msClassName; };  /*etc.*/   Is there a way to somehow convert this string to an actual type (class), so that BaseFactory wouldn't have to know all the possible Derived classes, and have if() for each one of them? Can I produce a class from this string?   I think this can be done in C# through Reflection. Is there something similar in C++?
51146
Here is an example of polymorphism from  (edited for readability):  // abstract base class #include &lt;iostream&gt; using namespace std;  class Polygon {     protected:         int width;         int height;     public:         void set_values(int a, int b) { width = a; height = b; }         virtual int area(void) =0; };  class Rectangle: public Polygon {     public:         int area(void) { return width * height; } };  class Triangle: public Polygon {     public:         int area(void) { return width * height / 2; } };  int main () {     Rectangle rect;     Triangle trgl;     Polygon * ppoly1 = &amp;rect;     Polygon * ppoly2 = &amp;trgl;     ppoly1-&gt;set_values (4,5);     ppoly2-&gt;set_values (4,5);     cout &lt;&lt; ppoly1-&gt;area() &lt;&lt; endl; // outputs 20     cout &lt;&lt; ppoly2-&gt;area() &lt;&lt; endl; // outputs 10     return 0; }   My question is how does the compiler know that ppoly1 is a Rectangle and that ppoly2 is a Triangle, so that it can call the correct area() function? It could find that out by looking at the "Polygon * ppoly1 = &rect;" line and knowing that rect is a Rectangle, but that wouldn't work in all cases, would it? What if you did something like this?  cout &lt;&lt; ((Polygon *)0x12345678)-&gt;area() &lt;&lt; endl;   Assuming that you're allowed to access that random area of memory.  I would test this out but I can't on the computer I'm on at the moment.  (I hope I'm not missing something obvious...)
31381
The following code says that passing the map as const into the operator[] method discards qualifiers: #include &lt;iostream&gt; #include &lt;map&gt; #include &lt;string&gt;  using namespace std;  class MapWrapper { public:     const int &amp;get_value(const int &amp;key) const {         return _map[key];     }  private:     map&lt;int, int&gt; _map; };  int main() {     MapWrapper mw;     cout &lt;&lt; mw.get_value(42) &lt;&lt; endl;     return 0; }  Is this because of the possible allocation that occurs on the map access? Can no functions with map accesses be declared const? MapWrapper.cpp:10: error: passing const std::map&lt;int, int, std::less&lt;int&gt;, std::allocator&lt;std::pair&lt;const int, int&gt; &gt; &gt; as this argument of  _Tp&amp; std::map&lt;_Key, _Tp, _Compare, _Alloc&gt;::operator[](const _Key&amp;)  [with _Key = int, _Tp = int, _Compare = std::less&lt;int&gt;,  _Alloc = std::allocator&lt;std::pair&lt;const int, int&gt; &gt;] discards qualifiers
22280
I've been writing C and C++ code for almost twenty years, but there's one aspect of these languages that I've never really understood. I've obviously used regular casts i.e.  MyClass *m = (MyClass *)ptr;   all over the place, but there seem to be two other types of casts, and I don't know the difference. What's the difference between the following lines of code?  MyClass *m = (MyClass *)ptr; MyClass *m = static_cast&lt;MyClass *&gt;(ptr); MyClass *m = dynamic_cast&lt;MyClass *&gt;(ptr);