Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>As @MSN mentioned, you need to use nested templates. In your case they take the form of:</p> <pre><code>template&lt;typename T, size_t nr, template &lt;typename, size_t&gt; class FruitType&gt; class Tree { ... }; </code></pre> <p>And they are used this way:</p> <pre><code>Tree&lt;double, 20, Apple&gt; someTree; </code></pre> <p>Real example from the code you have provided (compiles under VC++ 2010):</p> <pre><code>#include &lt;iostream&gt; #include &lt;vector&gt; template &lt;typename T, size_t numberofapples&gt; class Apples { public: Apples(std::vector&lt;T&gt; appleinfo1, std::vector&lt;T&gt; appleinfo2); protected: std::vector&lt;T&gt; apple_stats; std::vector&lt;T&gt; info1, info2; }; template &lt;typename T, size_t numberofapples&gt; Apples&lt;T, numberofapples&gt;::Apples(std::vector&lt;T&gt; appleinfo1, std::vector&lt;T&gt; appleinfo2) : apple_stats(numberofapples, 0){ for (size_t i = 0; i &lt; numberofapples; ++i) { apple_stats[i] = rand(); } info1 = appleinfo1; info2 = appleinfo2; } template &lt;typename T, size_t numberoffruitperbranch, template &lt;typename, size_t&gt; class FruitType&gt; class Tree { public: Tree(size_t numberofbranches, std::vector&lt;T&gt; commonfruitinfo1, std::vector&lt;T&gt; commonfruitinfo2); protected: std::vector&lt;FruitType&lt;T, numberoffruitperbranch&gt; &gt; branchset; }; template &lt;typename T, size_t numberoffruitperbranch, template &lt;typename, size_t&gt; class FruitType&gt; Tree&lt;T, numberoffruitperbranch, FruitType&gt;::Tree(size_t numberofbranches, std::vector&lt;T&gt; commonfruitinfo1, std::vector&lt;T&gt; commonfruitinfo2) { typename FruitType&lt;T, numberoffruitperbranch&gt; single_fruit(commonfruitinfo1, commonfruitinfo2); branchset.resize(numberofbranches, single_fruit); //in the un-abstracted version that has nothing to do with fruit, I'd then iterate over the vector and run some internal code on each one }; int main() { Tree&lt;double, 10, Apples&gt; someTree(20, std::vector&lt;double&gt;(), std::vector&lt;double&gt;()); return 0; } </code></pre>
 

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