Note that there are some explanatory texts on larger screens.

plurals
  1. POC++ Using a class template argument as a template argument for another type
    primarykey
    data
    text
    <p>I'm having this problem while writing my own HashTable. It all works, but when I try to templatize the thing, it gave me errors. I recreated the problem as follows: </p> <p>THIS CODE WORKS:</p> <pre><code>typedef double Item; class A { public: A() { v.push_back(pair&lt;string, Item&gt;("hey", 5.0)); } void iterate() { for(Iterator iter = v.begin(); iter != v.end(); ++iter) cout &lt;&lt; iter-&gt;first &lt;&lt; ", " &lt;&lt; iter-&gt;second &lt;&lt; endl; } private: vector&lt;pair&lt;string, double&gt; &gt; v; typedef vector&lt; pair&lt;string, double&gt; &gt;::iterator Iterator; }; </code></pre> <p>THIS CODE DOES NOT: </p> <pre><code>template&lt;typename ValueType&gt; class B { public: B(){} void iterate() { for(Iterator iter = v.begin(); iter != v.end(); ++iter) cout &lt;&lt; iter-&gt;first &lt;&lt; ", " &lt;&lt; iter-&gt;second &lt;&lt; endl; } private: vector&lt;pair&lt;string, ValueType&gt; &gt; v; typedef vector&lt; pair&lt;string, ValueType&gt; &gt;::iterator Iterator; }; </code></pre> <p>the error messages: g++ -O0 -g3 -Wall -c -fmessage-length=0 -omain.o ..\main.cpp</p> <p>..\main.cpp:50: error: type <code>std::vector&lt;std::pair&lt;std::string, ValueType&gt;, std::allocator&lt;std::pair&lt;std::string, ValueType&gt; &gt; &gt;' is not derived from type</code>B'</p> <p>..\main.cpp:50: error: ISO C++ forbids declaration of `iterator' with no type</p> <p>..\main.cpp:50: error: expected `;' before "Iterator"</p> <p>..\main.cpp: In member function `void B::iterate()':</p> <p>..\main.cpp:44: error: `Iterator' was not declared in this scope</p> <p>..\main.cpp:44: error: expected `;' before "iter"</p> <p>..\main.cpp:44: error: `iter' was not declared in this scope</p> <p>Does anybody know why this is happening? Thanks! </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload