声明和初始化问题
我正在尝试重写我之前编写的代码。 该代码使用 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;
}
这段代码(虽然它不做任何事情)有效...但是现在我已经实现了自己的类,并且希望也能够使用这些函数,但我不知道如何初始化它们。 所以这次我把它们写在一个不同的类中,称为求解器。
//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
然后是 cpp 文件
//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);
}
如果我向此类添加一个函数,例如调用 x.add(IloNumVar(env)); 的函数; 在第一个示例中,这将向 x(array) 添加一个变量,但是当我将它放在不同的类中时,我捕获“尝试实现空句柄”...
我知道我在主程序中所做的一切都是正确的,如果我在 h.file 中没有不同的 CPLEX 类,我也可以让它工作,但是我只能使用相同的模型一次,并且我想多次调用相同的模型。
h.file 中是否存在明显错误(除了缺少代码、析构函数等...)?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这段代码:
不是初始化类成员 - 它是在构造函数中创建局部变量,当构造函数退出时,局部变量将被销毁。你想要类似的东西:
then in main:
This code:
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:
then in main:
也许
更接近你想要的。
Perhaps
is closer to what you want.