Problems with declarations and initializations
I am trying to rewrite a code I have written earlier. The code uses cplex concert API;
#include <ilcplex/ilocplex.h>
using namespace std;
ILOSTLBEGIN
int main(){
IloEnv env;
IloModel model(env);
IloVarArray x(env);
IloCplex cplex(model);
return 0;
}
This code (though it doesn't do anything) works... However now i have implemented my own Class and would like to be able to use these functions as well but I don't know how to inizialize them. So this time I have written them in a differnet class called solver.
//solver.h
#ifndef solver_h
#define solver_h
#include <ilcplex/ilocplex.h>
class solver{
public:
IloModel model;
IloNumVarArray x;
IloRangeArray con;
IloCplex cplex;
solver();
solver~();
};
#endif
Then the cpp file
//solver.cpp
#include <ilcplex/ilocplex.h>
#include <vector>
using namespace std;
#include "solver.h"
ILOSTLBEGIN
solver::solver(){
IloEnv env;
IloModel model(env);
IloVarArray x(env);
IloCplex cplex(model);
}
If i add a function to this class e.g. a function that calls x.add(IloNumVar(env)); In the first example this would add an variable to the x(array), but when I have it in a different class I catch "tring to implement开发者_运维问答 empty handle"...
I know I'm doing everything right in the main program, and I also get it to work if I dont have the different Cplex classes in the h.file but then I can only use the same model once and i would want to call the same model several times.
Is there something clearly wrong here (Besides the lack of code, destructors, etc...) in the h.file or?
This code:
solver::solver(){
IloEnv env;
IloModel model(env);
IloVarArray x(env);
IloCplex cplex(model);
}
is not initialising your class members - it is creating local variables in the constructor, which will destroyed when the constructor exits. You want something like:
solver :: solver( IloEnv & env )
: model( env ), x( env ), cplex( model ) {
}
then in main:
int main() {
IloEnv env;
solver s( env ); // create solver object
}
Perhaps
solver::solver () : model (env), x (env), cplex (model)
{
}
is closer to what you want.
精彩评论