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