简体   繁体   English

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

[英]how to initialize a static struct in c++?

I have managed to initialize correct any variable of basic type(ie int, char, float etc) but when declaring a little complex variable all i can see is errors. 我已经设法初始化正确的任何基本类型的变量(即int,char,float等)但是当声明一个复杂的变量时,我能看到的只是错误。

In the header file timer.hi declare 在头文件中,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;
...
}

inside the timer.cpp file 在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;

}

when compiling the .cpp file i get the error: timer.cpp:7: error: expected initializer before '.' 在编译.cpp文件时,我得到错误:timer.cpp:7:错误:'。'之前的预期初始化程序。 token 代币

Any suggestions would be really helpful. 任何建议都会非常有用。

btw i use g++ 顺便说一句,我使用g ++

You don't separately define individual instance members within a static member. 您不会在静态成员中单独定义单个实例成员。

This should be enough: 这应该足够了:

AndroidTimerModel::Resources AndroidTimerModel::ResData;

You can use a struct initializer in C++, but only in the pre-C99 style (ie, you cannot use designated initializers). 您可以在C ++中使用struct初始化程序,但仅限于C99之前的样式(即,您不能使用指定的初始化程序)。 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). 指定的初始化程序 ,允许您指定要按名称初始化的成员,而不是依赖于声明顺序,在C99中引入,但目前不是任何C ++标准的一部分(通常假设C ++是超集) 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. 如果您愿意编写专门针对g ++的非可移植C ++代码,则可以始终使用与指定构造函数具有相同功能的GCC特定扩展 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. 此参考提供了对C上下文中两种类型的初始化的非常好的概述。

Here's an excerpt that shows both the earlier (without designators) and C99 styles: 这是一个摘录,显示了早期(没有指示符)和C99样式:

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. 初始化结构时,列表中的第一个初始化程序初始化第一个声明的成员(除非指定了指示符)(自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

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();

You need to declare and define a constructor for struct Resources . 您需要为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) {}
    ....
};

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

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

Is it AndroidTimerModel or AndroidTimerConcept, you can't use different names and expect the compiler to think they're the same thing. 是AndroidTimerModel还是AndroidTimerConcept,你不能使用不同的名称,并期望编译器认为它们是同一个东西。

You need to scope the name Resources, it's not in global scope, it's in the scope of the AndroidTimerModel class: 您需要将名称范围限定为资源,它不在全局范围内,它位于AndroidTimerModel类的范围内:

AndroidTimerModel::Resources AndroidTimerModel::ResData;

I suggest you give Resources a constructor: 我建议你给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;
};

And you can then define Res in your .cpp as: 然后,您可以在.cpp中将Res定义为:

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

Why is your struct part of a class? 为什么你的struct是一个类的一部分? I would make it global outside of the class. 我会在全班以外的地方做到全球化。

memset(&structname, 0, sizeof(structname)); will initialize your structure to 0. 将您的结构初始化为0。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM