在 C++ 中声明全局变量时出现问题
我在类中有一个全局变量:
IloModel model;
它将在类的多个函数之间共享。
在其中一个函数中,我将像这样初始化该模型:
model(env);
我收到错误: 错误 1 错误 C2064:术语不计算为采用 1 个参数的函数
如果我在函数中写入,则此方法有效:
IEnv env;
IloModel model(env);
但如果模型对象是全局声明的,则无效。
请建议如何使模型对象全局化,以便同一对象可以在多个函数之间共享?
I have a global variable in the class:
IloModel model;
that is going to be shared amongst several functions of the class.
In one of the functions, I am going to initialize this model like:
model(env);
I get the error:
Error 1 error C2064: term does not evaluate to a function taking 1 arguments
This works if I write in the function:
IEnv env;
IloModel model(env);
but not if the model object is declared globally.
Please advice how to make model object global so same object can be shared amongst several functions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
当你这样做时,
它正在使用默认构造函数进行初始化;所以
IloModel()
您需要做的是
出现错误可能是因为它正在寻找具有一个参数的函数模型,但没有找到。
when you do
It is being initialised with the default constructor; so
IloModel()
What you need to do is
There error is probably because it is looking for a function model with one paramatere and not finding one.
不确定我理解你所说的“全球”是什么意思,因为你似乎在谈论班级的成员。无论如何,如果您的数据是可复制的,您可以这样做
,这将创建一个新模型并将其复制到当前模型上。
Not sure I understand what you mean with "global" because it seems you're talking about a member of the class. Anyway if your data is copyable you can do
this will create a new model and copy it over the current one.
为什么不全局声明 env 然后直接用 env 初始化模型?
换句话说,将其放在全局中:
否则,您无法调用构造函数来初始化模型,您需要在
IloModel
类上使用另一个 init 方法来在构造后指定IEnv
。Why don't you declare env globally also and then directly initialize model with env?
In other words, put this globally:
Otherwise, you cannot call the constructor to initialize model, you need another init method on
IloModel
class to specify theIEnv
after the construction.您不能在稍后的时间点调用对象的
model
的构造函数。您需要在定义对象本身时执行此操作。如果env
也是全局的,那么您可以执行IModel model(env)
。You can not call the constructor of
model
of object at a later point of time. You need to do it at the time of definition of the object itself. Ifenv
is also global then you can doIModel model(env)
.您在模型类上调用的函数是它的构造函数。作为类的成员(类的全局成员,但肯定不是应用程序,除非它是静态的且可访问的),您可以构造它一次,并且位于该类的初始化程序列表中有它,例如:
现在,我根本不知道
IEnv
是什么(或与此相关的模型),因此如果逻辑表明您需要使用每次使用时都有不同的IEnv
。The function you are calling on the model class, is it's constructor. As a member of your class (global to the class, but certainly not the application unless it is static and accessible), you get to construct it once and that is in the initializer-list of the class that has it, e.g:
Now, I do not know what
IEnv
is at all (or the model for that matter) so this might not work for you if the logic dictates that you need to construct model with a differentIEnv
each time you use it.在最后一个示例中,您调用构造函数,这是一个用于初始化对象的特殊函数。第一个示例尝试调用对象
model
上的operator()
,如果类IloModel
没有定义,则不会存在。但是,您可以调用
IloModel(env);
这将创建一个临时对象并立即再次销毁它,呵呵。In your last example, you call the constructor, a special function to initialize the object. The first example tries to call the
operator()
on your objectmodel
, and if the classIloModel
does not define one, it doesn't exist.You could, however, call
IloModel(env);
which would create an temporary object and immediately destroy it again, heh.