tags:

views:

55

answers:

2

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?

+6  A: 

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
}
anon
Ah thanks! Thought my question was way too long and messy for someone to read it, but clearly not!
mrbuxley
+1  A: 

Perhaps

solver::solver () : model (env), x (env), cplex (model)
{
}

is closer to what you want.

Brian Hooper
Although it fails, because `env` isn't defined.
sbi
Quite correct. Mr Butterworth's new edit has it.
Brian Hooper