Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to invoke non-default constructor for template class?
    primarykey
    data
    text
    <p>I have the following code structure (<code>Resource</code> and <code>Parameter</code> are empty classes):</p> <p>Solver.cpp</p> <pre><code>#include "Solver.h" #include "ValueFunction.h" using namespace std; template&lt;typename T&gt; Solver&lt;T&gt;::Solver(vector&lt;vector&lt;Resource&gt; &gt;&amp; resources, const Parameter&amp; params) : states(resources.size()) { for (int i=0; i&lt;resources.size(); i++) { states[i] = State&lt;T&gt;(resources[i], params); } } // Explicit class declaration template class Solver&lt;ValueFunction&gt;; </code></pre> <p>Solver.h</p> <pre><code>#ifndef SOLVER_H_ #define SOLVER_H_ #include &lt;vector&gt; #include "Resource.h" #include "Parameter.h" #include "State.h" template&lt;typename T&gt; class Solver { public: Solver( std::vector&lt;std::vector&lt;Resource&gt; &gt;&amp; resources, const Parameter&amp; params ); private: std::vector&lt;State&lt;T&gt; &gt; states; }; #endif /* SOLVER_H_ */ </code></pre> <p>State.cpp</p> <pre><code>#include "State.h" #include "ValueFunction.h" using namespace std; template&lt;typename T&gt; State&lt;T&gt;::State(vector&lt;Resource&gt;&amp; _resources, const Parameter&amp; params) : resources(_resources), valfuncs(_resources.size(), T(params)) { } template class State&lt;ValueFunction&gt;; </code></pre> <p>State.h</p> <pre><code>#ifndef STATE_H_ #define STATE_H_ #include &lt;vector&gt; #include "Parameter.h" #include "Resource.h" template&lt;typename T&gt; class State { public: State() {}; State(std::vector&lt;Resource&gt;&amp; _resources, const Parameter&amp; params); ~State() {}; private: std::vector&lt;Resource&gt; resources; std::vector&lt;T&gt; valfuncs; }; #endif /* STATE_H_ */ </code></pre> <p>ValueFunction.cpp</p> <pre><code>#include "ValueFunction.h" ValueFunction::ValueFunction(const Parameter&amp; _params) : params(_params) { } </code></pre> <p>ValueFunction.h</p> <pre><code>#ifndef VALUEFUNCTION_H_ #define VALUEFUNCTION_H_ #include "Parameter.h" class ValueFunction { public: ValueFunction(const Parameter&amp; _params); private: const Parameter&amp; params; }; #endif /* VALUEFUNCTION_H_ */ </code></pre> <p>With the following call:</p> <pre><code>#include "Solver.h" #include "State.h" #include "ValueFunction.h" #include "Parameter.h" using namespace std; int main(int argc, char *argv[]) { Parameter params; vector&lt;vector&lt;Resource&gt; &gt; resources(4); Solver&lt;ValueFunction&gt; sol(resources, params); return 0; } </code></pre> <p>And I get the following error:</p> <pre><code>Solver.cpp:18:16: instantiated from here ValueFunction.h:6:21: error: non-static reference member ‘const Parameter&amp; ValueFunction::params’, can't use default assignment operator </code></pre> <p>How can I invoke the non-default constructor for <code>ValueFunction</code> correctly, or is there an other way to initialize an <code>std::vector</code> with a non-default constructor (passing a constant reference)?</p> <p><strong>Update</strong></p> <p>The error is explained in this <a href="https://stackoverflow.com/questions/634662/non-static-const-member-cant-use-default-assignment-operator">post</a>. But the workaround for my problem is not entirly clear. Any suggestions?</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.
 

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