初始化静态变量时出现语法错误

发布于 2024-12-20 18:02:05 字数 398 浏览 1 评论 0原文

定义了两个类..

    class Dictionary
    {
    public:
        Dictionary();
        Dictionary(int i);
// ...
    };

    class Equation
    {
        static Dictionary operator_list(1);
// ...

    };

问题是,每当我编译它时,我都会收到一条奇怪的错误消息

错误 C2059:语法错误:“常量”

但是当我在operator_list上使用默认构造函数时它编译得很好。

There are two classes defined..

    class Dictionary
    {
    public:
        Dictionary();
        Dictionary(int i);
// ...
    };

and

    class Equation
    {
        static Dictionary operator_list(1);
// ...

    };

but the problem is, whenever I compile this, I get a weird error message

error C2059: syntax error : 'constant'

But it compiles well when I use the default constructor on operator_list.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

甜点 2024-12-27 18:02:05

在 C++ 中,不能将声明和初始化结合起来。当您没有指定operator_list的构造函数参数时,您不会调用它的默认构造函数:您只需声明它即可。您还需要在相应的 C++ 文件中对其进行初始化,如下所示:

Equation.h

class Equation {
    static Dictionary operator_list;
};

Equation.cpp:

Dictionary Equation::operator_list(1);

请注意 CPP 文件中缺少 static:它不是有意设计的。编译器已经从声明中知道 operator_list 是静态的。

编辑:您可以选择整数和枚举类型的静态常量成员:您可以在 CPP 文件中初始化它们,如上例所示,或者您可以在标头中给它们一个值。您仍然需要在 C++ 文件中定义该成员,但不能在定义时为其赋值。

In C++ you cannot combine declaration and initialization. When you do not specify constructor parameters of operator_list, you do not call its default constructor: you simply declare it. You need to also initialize it in the corresponding C++ file, like this:

Equation.h

class Equation {
    static Dictionary operator_list;
};

Equation.cpp:

Dictionary Equation::operator_list(1);

Note the absence of static in the CPP file: it is not there by design. The compiler already knows from the declaration that operator_list is static.

Edit: You have a choice with static constant members of integral and enumerated types: you can initialize them in the CPP file as in the example above, or you can give them a value in the header. You still need to define that member in your C++ file, but you must not give it a value at the definition time.

如歌彻婉言 2024-12-27 18:02:05

static Dictionary operator_list(); 是一个函数签名,声明一个返回 Dictionary 且不带任何参数的函数,这就是编译器允许您这样做的原因。

static Dictionary operator_list(1); 失败的原因是您无法在类的声明中设置复杂类型的值。您需要在其他地方执行此操作(例如在 .cpp 中)

有关更多信息,请参阅这篇文章: https://stackoverflow.com/a /3792427/103916

static Dictionary operator_list(); is a function signature declaring a function returning a Dictionary and taking no arguments, that's why your compiler let you do it.

The reasons static Dictionary operator_list(1); fails is because you can't set a value of an complex type in the declaration of your classes. You need to do this elsewhere (e.g. in the .cpp )

For more information, see this post : https://stackoverflow.com/a/3792427/103916

べ映画 2024-12-27 18:02:05
#include <iostream>
using namespace std;

class Dictionary
{
public:
    Dictionary() {}
    Dictionary(int i):page(i) {}
    void display() { cout << "page is " << page << endl; }
private:
    int page;
};

class Equation
{
    public:
    static Dictionary operator_list;

};

Dictionary Equation::operator_list(1); // static members must be initialized this way...

int main()
{
Equation::operator_list.display();
}

输出是:

page is 1
#include <iostream>
using namespace std;

class Dictionary
{
public:
    Dictionary() {}
    Dictionary(int i):page(i) {}
    void display() { cout << "page is " << page << endl; }
private:
    int page;
};

class Equation
{
    public:
    static Dictionary operator_list;

};

Dictionary Equation::operator_list(1); // static members must be initialized this way...

int main()
{
Equation::operator_list.display();
}

Output is:

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