如何在c++中初始化静态结构?

发布于 2024-11-02 13:17:40 字数 1065 浏览 6 评论 0原文

我已经成功地初始化了正确的任何基本类型变量(即 int、char、float 等),但是当声明一个复杂的变量时,我只能看到错误。

在编译 .cpp 文件时,在头文件timer.hi

class AndroidTimerConcept {
...
private:
    //struct that holds the necessary info for every event
    struct Resources{
        timer_delegate_t membFunct;
        void *data;
        int size;
        millis_t time;
    };
    //declaring an array of 10 Resources structs
    static struct Resources ResData;
    static int best;
...
}

在timer.cpp文件中

#include <iostream>
#include "timer.h"
using namespace std;


int AndroidTimerModel::best=1000;
struct Resources AndroidTimerModel::ResData.size; //line 17!!
//constructor that initializes all the necessary variables

AndroidTimerModel::AndroidTimerModel()
{
    signal(SIGALRM,signalHandler);

    for(int i=0; i<MAX_EVENTS; i++)
    {
        //ResData.data=NULL;
        ResData.size=-1;
        //ResData.time=-1;
    }

    best=1000;

}

声明时出现错误: timer.cpp:7: 错误: '.' 之前需要初始化程序token

任何建议都会非常有帮助。

顺便说一句我使用 g++

I have managed to initialize correct any variable of basic type(i.e. int, char, float etc) but when declaring a little complex variable all i can see is errors.

In the header file timer.h i declare

class AndroidTimerConcept {
...
private:
    //struct that holds the necessary info for every event
    struct Resources{
        timer_delegate_t membFunct;
        void *data;
        int size;
        millis_t time;
    };
    //declaring an array of 10 Resources structs
    static struct Resources ResData;
    static int best;
...
}

inside the timer.cpp file

#include <iostream>
#include "timer.h"
using namespace std;


int AndroidTimerModel::best=1000;
struct Resources AndroidTimerModel::ResData.size; //line 17!!
//constructor that initializes all the necessary variables

AndroidTimerModel::AndroidTimerModel()
{
    signal(SIGALRM,signalHandler);

    for(int i=0; i<MAX_EVENTS; i++)
    {
        //ResData.data=NULL;
        ResData.size=-1;
        //ResData.time=-1;
    }

    best=1000;

}

when compiling the .cpp file i get the error:
timer.cpp:7: error: expected initializer before ‘.’ token

Any suggestions would be really helpful.

btw i use g++

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

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

发布评论

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

评论(6

南烟 2024-11-09 13:17:40

您不必在静态成员中单独定义各个实例成员。

这应该足够了:

AndroidTimerModel::Resources AndroidTimerModel::ResData;

You don't separately define individual instance members within a static member.

This should be enough:

AndroidTimerModel::Resources AndroidTimerModel::ResData;
空名 2024-11-09 13:17:40

您可以在 C++ 中使用结构体初始值设定项,但仅限于 C99 之前的风格(即,您不能使用指定的初始值设定项)。 指定初始化,它允许您指定要按名称初始化的成员,而不是依赖声明顺序,它是在 C99 中引入的,但目前还不是任何 C++ 标准的一部分(与常见的假设是 C++ 是 C 的超集)。

如果您愿意编写专门针对 g++ 的不可移植 C++ 代码,则始终可以使用 GCC 特定扩展,它与指定构造函数具有相同的功能。语法如下:

struct value_t my_val = { member_a: 1, member_b: 1.2f };

此参考 提供了两种类型的很好的概述C 上下文中的初始化。

以下摘录显示了早期(无指示符)和 C99 样式:

初始化结构体时,列表中的第一个初始化器
初始化第一个声明的成员(除非指示符是
指定)(自 C99 起),以及所有后续初始化程序,没有
指示符 (C99 起) 初始化之后声明的结构成员
由前一个表达式初始化的值。

struct point {double x,y,z;} p = {1.2, 1.3}; // p.x=1.2, p.y=1.3, p.z=0.0
div_t answer = {.quot = 2, .rem = -1 };      // order of elements in div_t may vary

在某些情况下,您可能需要编写一些代码来初始化结构,在这种情况下,您可以使用函数的结果,例如:

struct Resources AndroidTimerModel::ResData = function_that_acts_like_a_constructor();

You can use a struct initializer in C++, but only in the pre-C99 style (i.e, you cannot use designated initializers). Designated intializers, which allow you to specify the members to be initialized by name, rather than relying on declaration order, were introduced in C99, but aren't part of any C++ standard at the moment (belying the common assumption that C++ is a superset of C).

If you are willing to write non-portable C++ code that specifically targets g++, you can always use the GCC-specific extension which has the same functionality as designated constructors. The syntax is like this:

struct value_t my_val = { member_a: 1, member_b: 1.2f };

This reference provides a pretty good overview of both types of initialization in the C context.

Here's an excerpt that shows both the earlier (without designators) and C99 styles:

When initializing a struct, the first initializer in the list
initializes the first declared member (unless a designator is
specified) (since C99), and all subsequent initializers without
designators (since C99) initialize the struct members declared after
the one initialized by the previous expression.

struct point {double x,y,z;} p = {1.2, 1.3}; // p.x=1.2, p.y=1.3, p.z=0.0
div_t answer = {.quot = 2, .rem = -1 };      // order of elements in div_t may vary

In some cases you may need to write some code to initialize a structure, and in this case you can use the result of a function, like:

struct Resources AndroidTimerModel::ResData = function_that_acts_like_a_constructor();
画▽骨i 2024-11-09 13:17:40

您需要为 struct Resources 声明并定义一个构造函数。
例如

struct Resources{
    timer_delegate_t membFunct;
    void *data;
    int size;
    millis_t time;
    Resources():membFunct(0), data(0), size(0), time(0) {}
    ....
};

You need to declare and define a constructor for struct Resources.
eg

struct Resources{
    timer_delegate_t membFunct;
    void *data;
    int size;
    millis_t time;
    Resources():membFunct(0), data(0), size(0), time(0) {}
    ....
};
东京女 2024-11-09 13:17:40

您需要初始化整个结构变量,如下所示:

AndroidTimerConcept::Resources AndroidTimerModel::ResData = { NULL, NULL, 0, 0 };

You need to initialise the whole struct variable, something like this:

AndroidTimerConcept::Resources AndroidTimerModel::ResData = { NULL, NULL, 0, 0 };
愿与i 2024-11-09 13:17:40

无论是 AndroidTimerModel 还是 AndroidTimerConcept,您都不能使用不同的名称并期望编译器认为它们是相同的东西。

您需要确定名称 Resources 的范围,它不在全局范围内,而是在 AndroidTimerModel 类的范围内:

AndroidTimerModel::Resources AndroidTimerModel::ResData;

我建议您为 Resources 提供一个构造函数:

struct Resources{
    Resources(timer_delegate_t aMembFunct, void* aData, int aSize, millis_t aTime )
      : membFunc(aMembFunct)
      , data(aData)
      , size(aSize)
      , time(aTime)
    {}
    timer_delegate_t membFunct;
    void *data;
    int size;
    millis_t time;
};

然后您可以在 .cpp 中将 Res 定义为:

AndroidTimerModel::Resources AndroidTimerModel::ResData(/* params here */);

Is it AndroidTimerModel or AndroidTimerConcept, you can't use different names and expect the compiler to think they're the same thing.

You need to scope the name Resources, it's not in global scope, it's in the scope of the AndroidTimerModel class:

AndroidTimerModel::Resources AndroidTimerModel::ResData;

I suggest you give Resources a constructor:

struct Resources{
    Resources(timer_delegate_t aMembFunct, void* aData, int aSize, millis_t aTime )
      : membFunc(aMembFunct)
      , data(aData)
      , size(aSize)
      , time(aTime)
    {}
    timer_delegate_t membFunct;
    void *data;
    int size;
    millis_t time;
};

And you can then define Res in your .cpp as:

AndroidTimerModel::Resources AndroidTimerModel::ResData(/* params here */);
明月松间行 2024-11-09 13:17:40

为什么你的struct是类的一部分?我会让它在课堂之外变得全球化。

memset(&structname, 0, sizeof(structname)); 将把你的结构初始化为 0。

Why is your struct part of a class? I would make it global outside of the class.

memset(&structname, 0, sizeof(structname)); will initialize your structure to 0.

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