在 C++ 中声明全局变量时出现问题

发布于 2024-10-15 19:13:04 字数 370 浏览 2 评论 0原文

我在类中有一个全局变量:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

傲性难收 2024-10-22 19:13:04

当你这样做时,

IloModel model;

它正在使用默认构造函数进行初始化;所以 IloModel()

您需要做的是

model = IloModel(env);

出现错误可能是因为它正在寻找具有一个参数的函数模型,但没有找到。

when you do

IloModel model;

It is being initialised with the default constructor; so IloModel()

What you need to do is

model = IloModel(env);

There error is probably because it is looking for a function model with one paramatere and not finding one.

写下不归期 2024-10-22 19:13:04

不确定我理解你所说的“全球”是什么意思,因为你似乎在谈论班级的成员。无论如何,如果您的数据是可复制的,您可以这样做

model = IloModel(env);

,这将创建一个新模型并将其复制到当前模型上。

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

model = IloModel(env);

this will create a new model and copy it over the current one.

盛装女皇 2024-10-22 19:13:04

为什么不全局声明 env 然后直接用 env 初始化模型?
换句话说,将其放在全局中:

IEnv env;
IloModel model(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:

IEnv env;
IloModel model(env);

Otherwise, you cannot call the constructor to initialize model, you need another init method on IloModel class to specify the IEnv after the construction.

逆光下的微笑 2024-10-22 19:13:04

您不能在稍后的时间点调用对象的 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. If env is also global then you can do IModel model(env).

咆哮 2024-10-22 19:13:04

您在模型类上调用的函数是它的构造函数。作为类的成员(类的全局成员,但肯定不是应用程序,除非它是静态的且可访问的),您可以构造它一次,并且位于该类的初始化程序列表中有它,例如:

class MyClass
{
private:
    IloModel model;

public:
     MyClass(IEnv _env) : model(_env) // constructs model here, passing env
     {
     };

     void someFunction()
     {
         // use model here
     };
}; // eo class MyClass

现在,我根本不知道 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:

class MyClass
{
private:
    IloModel model;

public:
     MyClass(IEnv _env) : model(_env) // constructs model here, passing env
     {
     };

     void someFunction()
     {
         // use model here
     };
}; // eo class MyClass

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 different IEnv each time you use it.

阿楠 2024-10-22 19:13:04

在最后一个示例中,您调用构造函数,这是一个用于初始化对象的特殊函数。第一个示例尝试调用对象 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 object model, and if the class IloModel 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文