title: Adding constraints to a CPLEX model in C++
body: <p>I'm coding a MILP in C++ by using the CPLEX library and I am having problems when adding the constraints to the model. The code is quite long, so here I just include the general structure of the code and one of the constraints (and the variables involved).</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;fstream&gt;
  #include &lt;cmath&gt;
  #include &lt;vector&gt;
  #include &lt;string&gt;
  #include "ilcplex/ilocplex.h"
  
  using namespace std;
  
  ILOSTLBEGIN
  
  int main(){
  
      //CPLEX environment and definition of the modelling object
      IloEnv env;
      IloModel model(env);
  
      //Define the multi dimensional arrays for float and bool variables
      typedef IloArray&lt;IloBoolVarArray&gt; BoolVar2D;
      typedef IloArray&lt;IloFloatVarArray&gt; FloatVar2D;
      typedef IloArray&lt;IloArray&lt;IloBoolVarArray&gt; &gt; BoolVar3D;
      typedef IloArray&lt;IloArray&lt;IloFloatVarArray&gt; &gt; FloatVar3D;
      typedef IloArray&lt;IloArray&lt;IloArray&lt;IloBoolVarArray&gt; &gt; &gt; BoolVar4D;
  
      //Definition of the variable involved in the constraint
      FloatVar3D U(env, I); //all alternatives except the opt-out
      for(int i=0; i &lt; I; i++){
          U[i] = FloatVar2D(env, N);
          for(int n=0; n &lt; N; n++){
              U[i][n] = IloFloatVarArray(env, R);
          }
      }
  
      //Construction of the constraint (chi, beta, lambda, p are parameters)
      for(int i =0; i &lt; I; i++){
          for(int n=0; n &lt; N; n++){
              int L_in = L[i][n];
              for(int r=0; r &lt; R; r++){
                  IloExpr sum(env);
                  sum += chi[i][n][r];
                  for(int l=0; l &lt; L_in; l++){
                      sum += beta[i][n] * lambda[i][n][l] * p[i][n][l];
                  }
                  model.add(U[i][n][r] == sum);
              }
          }
      }
  
      env.end();
  
  }
  </code></pre>
  
  <p>When running the code I get the following error message:</p>
  
  <pre><code>libc++abi.dylib: terminating with uncaught exception of type IloWrongUsage
  </code></pre>
  
  <p>Does anyone know what's wrong with the definition of the constraints in this way? I tried the same way for simpler problems and it worked.</p>
  
  <p>Thanks!</p>