# Decision Optimization

View Only

## Cplex with C++ #### Shouwei LiWed January 26, 2022 10:30 AM #### Renaud DumeurThu January 27, 2022 04:29 AMBest Answer • #### 1.  Cplex with C++

Posted Wed January 26, 2022 10:30 AM

Hi there,

I am new to cplex and come across a question.

Could you please show me how to solve this error?

Thanks a lot!

``````/* cplex code
range C = 1..N;
range Y = 1..10;
range C_D = 1..(N+1);
dvar int+ q[C_D][Y];

int d[C] = ...;
dvar boolean y[C_D][C_D][Y]

forall(j in C, m in Y)
d[j]*sum(i in C_D) y[i][j][m] <= q[j][m];

*/

//I translate this constraint to C++ as following:
for (int j = 0; j < C; ++j)
for (int m = 0; m < Y; ++m) {
IloExpr expr {env};
for (int i = 0; i < V; ++i)
if (y[i][j][m] <= q[j][m]) // error: Cannot convert "IloConstraint" to bool
expr += (y[i][j][m] <= q[j][m]);
}

``````

------------------------------
Shouwei Li
------------------------------

• #### 2.  RE: Cplex with C++ Best Answer

Posted Thu January 27, 2022 04:29 AM
Dear Shouwei,

The expression :

``y[i][j][m] <= q[j][m]​``

cannot be used in an if statement since it is of type IloConstraint. The reason for that is that y[i][j][m] is a model object for which the  <= operator has been overloaded, and so returns an IloConstraint.
You shouldn't declare y (nor q) as to contain model objects but as plain C++ 3d  (or 2d for q) matrices.
I hope this helps,
Cheers,

------------------------------
Renaud Dumeur
------------------------------

• #### 3.  RE: Cplex with C++

Posted Thu January 27, 2022 02:40 PM
Hi Renaud,