如何在c++中初始化静态结构?
我已经成功地初始化了正确的任何基本类型变量(即 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您不必在静态成员中单独定义各个实例成员。
这应该足够了:
You don't separately define individual instance members within a static member.
This should be enough:
您可以在 C++ 中使用结构体初始值设定项,但仅限于 C99 之前的风格(即,您不能使用指定的初始值设定项)。 指定初始化,它允许您指定要按名称初始化的成员,而不是依赖声明顺序,它是在 C99 中引入的,但目前还不是任何 C++ 标准的一部分(与常见的假设是 C++ 是 C 的超集)。
如果您愿意编写专门针对 g++ 的不可移植 C++ 代码,则始终可以使用 GCC 特定扩展,它与指定构造函数具有相同的功能。语法如下:
此参考 提供了两种类型的很好的概述C 上下文中的初始化。
以下摘录显示了早期(无指示符)和 C99 样式:
在某些情况下,您可能需要编写一些代码来初始化结构,在这种情况下,您可以使用函数的结果,例如:
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:
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:
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
声明并定义一个构造函数。例如
You need to declare and define a constructor for struct
Resources
.eg
您需要初始化整个结构变量,如下所示:
You need to initialise the whole struct variable, something like this:
无论是 AndroidTimerModel 还是 AndroidTimerConcept,您都不能使用不同的名称并期望编译器认为它们是相同的东西。
您需要确定名称 Resources 的范围,它不在全局范围内,而是在 AndroidTimerModel 类的范围内:
我建议您为 Resources 提供一个构造函数:
然后您可以在 .cpp 中将 Res 定义为:
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:
I suggest you give Resources a constructor:
And you can then define Res in your .cpp as:
为什么你的
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.