While searching for an answer to another  I developed a a helper class that would work with any kind of container of integers. Specifically, my class would lookup container values based on some criteria. In order to work with different types of containers, my class obviously needs to operate not on containers themselves but on their iterators. I don't need to reference containers anywhere in my class, I use only iterators and only in class constructor.  // Header file.   class MyLookup {     public:      template&lt;typename ForwardIt&gt;   // Forward iterator though sequence of integers     MyLookup(ForwardIt begin, ForwardIt end)     ...  }   // Implementation file   template&lt;typename ForwardIt&gt;  MyLookup::MyLookup(ForwardIt begin, ForwardIt end) {  ...  }   // Use file  std::vector&lt;int&gt; foo; ... MyLookup lookup(foo.begin(), foo.end());   While using CLang the initial compiler pass succeeds but then I get linker errors:  Undefined symbols for architecture x86_64:   "MyLookup::MyLookup >(std::__1::__wrap_iter, std::__1::__wrap_iter)", referenced from: ...  Any idea what am I doing wrong?
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.)
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.
1488
Can anyone explain why following code won't compile? At least on g++ 4.2.4.  And more interesting, why it will compile when I cast MEMBER to int?  #include &lt;vector&gt;  class Foo {   public:       static const int MEMBER = 1;   };  int main(){       vector&lt;int&gt; v;       v.push_back( Foo::MEMBER );       // undefined reference to `Foo::MEMBER'     v.push_back( (int) Foo::MEMBER ); // OK       return 0; }
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?
26933
Does the std::stack in the C++ STL expose any iterators of the underlying container or should I use that container directly?
7396
Why the following example prints "0" and what must change for it to print "1" as I expected ?  #include &lt;iostream&gt; struct base {    virtual const int value() const {       return 0;    }    base() {       std::cout &lt;&lt; value() &lt;&lt; std::endl;    }    virtual ~base() {} };  struct derived : public base {    virtual const int value() const {       return 1;    } };  int main(void) {    derived example; }
25742
I'm interested: What is C#'s analog of std::pair in C++? I found System.Web.UI.Pair class, but I'd prefer something template-based.  Thank you!
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.
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
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?
18220
I'm writing some cross-platform code between Windows and Mac.  If list::end() "returns an iterator that addresses the location succeeding the last element in a list" and can be checked when traversing a list forward,  what is the best way to traverse backwards?  This code workson the Mac but not on Windows (can't decrement beyond first element):  list&lt;DVFGfxObj*&gt;::iterator iter = m_Objs.end(); for (iter--; iter!=m_Objs.end(); iter--)// By accident discovered that the iterator is circular ? { }   this works on Windows:  list&lt;DVFGfxObj*&gt;::iterator iter = m_Objs.end();     do{         iter--;     } while (*iter != *m_Objs.begin());   Is there another way to traverse backward that could be implemented in a for loop?
17104
Consider these classes.  class Base {    ... };  class Derived : public Base {    ... };   this function  void BaseFoo( std::vector&lt;Base*&gt;vec ) {     ... }   And finally my vector  std::vector&lt;Derived*&gt;derived;   I want to pass derived to function BaseFoo, but the compiler doesn't let me. How do I solve this, without copying the whole vector to a std::vector&lt;Base*&gt;?
20526
It is to my understanding that one should use a forward-class declaration in the event ClassA needs to include a ClassB header, and ClassB needs to include a ClassA header to avoid any circular inclusions. I also understand that an #import is a simple ifndef so that an include only happens once.  My inquiry is this: When does one use #import and when does one use @class? Sometimes if I use a @class declaration, I see a common compiler warning such as the following:     warning: receiver 'FooController' is a forward class and corresponding @interface may not exist.   Would really love to understand this, versus just removing the @class forward-declaration and throwing an #import in to silence the warnings the compiler is giving me.
26298
std::map&lt;std::string, std::string&gt; myMap;  std::map&lt;std::string, std::string&gt;::iterator i = m_myMap.find(some_key_string); if(i == m_imagesMap.end())     return NULL;  string *p = &amp;i-&gt;first;   Is the last line valid?  I want to store this pointer p somewhere else, will it be valid for the whole program life?  But what will happen if I add some more elements to this map (with other unique keys) or remove some other keys, won’t it reallocate this string (key-value pair), so the p will become invalid?
29529
I am a big fan of letting the compiler do as much work for you as possible.  When writing a simple class the compiler can give you the following for 'free':   A default (empty) constructor A copy constructor A destructor An assignment operator (operator=)   But it cannot seem to give you any comparison operators - such as operator== or operator!=.  For example:  class foo { public:     std::string str_;     int n_; };  foo f1;        // Works foo f2(f1);    // Works foo f3; f3 = f2;       // Works  if (f3 == f2)  // Fails { }  if (f3 != f2)  // Fails { }   Is there a good reason for this?  Why would performing a member-by-member comparison be a problem?  Obviously if the class allocates memory then you'd want to be careful, but for a simple class surely the compiler could do this for you?
59275
Consider:  // member data omitted for brevity  // assume that "setAngle" needs to be implemented separately // in Label and Image, and that Button does need to inherit // Label, rather than, say, contain one (etc)  struct Widget {     Widget&amp; move(Point newPos) { pos = newPos; return *this; } };  struct Label : Widget {     Label&amp; setText(string const&amp; newText) { text = newText; return *this; }     Label&amp; setAngle(double newAngle) { angle = newAngle; return *this; } };  struct Button : Label {     Button&amp; setAngle(double newAngle) {         backgroundImage.setAngle(newAngle);         Label::setAngle(newAngle);         return *this;     } };  int main() {     Button btn;      // oops: Widget::setText doesn't exist     btn.move(Point(0,0)).setText("Hey");      // oops: calling Label::setAngle rather than Button::setAngle     btn.setText("Boo").setAngle(.5);  }   Any techniques to get around these problems?  Example: using template magic to make Button::move return Button&amp; or something.  edit It has become clear that the second problem is solved by making setAngle virtual.  But the first problem remains unsolved in a reasonable fashion!  edit: Well, I guess it's impossible to do properly in C++. Thanks for the efforts anyhow.
62585
Given the following example, why do I have to explicitly use the statement b-&gt;A::DoSomething() rather than just b-&gt;DoSomething()?  Shouldn't the compiler's overload resolution figure out which method I'm talking about?  I'm using Microsoft VS 2005. (Note: using virtual doesn't help in this case.)  class A {   public:     int DoSomething() {return 0;}; };  class B : public A {   public:     int DoSomething(int x) {return 1;}; };  int main() {   B* b = new B();   b-&gt;A::DoSomething();    //Why this?   //b-&gt;DoSomething();    //Why not this? (Gives compiler error.)   delete b;   return 0; }