如何在全局范围内初始化struct/class [英] How to initialize struct/class on the global scope

查看:92
本文介绍了如何在全局范围内初始化struct/class的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在知道如何在全局范围内初始化结构.

I do now know how to initialize structures on the global scope.

以下是示例代码:

#include<GL/glut.h>
struct A
{
    int x;
};
struct A a;
a.x=6;
int main()
{}

我在Ubuntu 11.10上编译该程序时,出现以下错误:

And I am on Ubuntu 11.10, when I compile this program, I get the following errors:

error: ‘a’ does not name a type

我不知道为什么会发生这种情况.我想知道如何将复杂的参数传递给某些回调函数?

I have no idea why this could happen. I am wondering how to pass a complex parameter to some callback function?

非常感谢

推荐答案

我在Ubuntu 11.10上编译该程序时,出现以下错误:错误:"a"未命名类型

And I am on Ubuntu 11.10, when I compile this program, I get the following errors: error: ‘a’ does not name a type

编译器通过此消息告诉您,结构成员的分配不能在全局范围内发生.如果要初始化a要么写

The compiler tells you with this message, that assignments to struct members can not happen in the global scope. If you want to initialize a either write

struct A a = {6};

或使用更新的语法

struct A a = {.x = 6};

或在程序启动后尽早(即在main的开头)进行初始化分配.

or do the initialization assignment early after program start (i.e. at the beginning of main).

BTW:这与GLUT或任何其他标头无关.这是语言规范.

BTW: This has nothing to do with GLUT or any other header. This is a language specification thing.

我想知道如何将复杂的参数传递给某些回调函数?

I am wondering how to pass a complex parameter to some callback function?

好吧,对于GLUT回调,这将很困难,因为GLUT不允许您指定用户定义的回调数据.您可以使用 ffcall 库创建原位的闭包,然后将其传递给GLUT.但是要考虑以下因素:一旦碰到这堵墙,就该放弃GLUT了. GLUT不是OpenGL开发所必需的,决不是要成为复杂应用程序的基础.所以那就不要使用它.

Well, in the case of GLUT callbacks it's going to be difficult, because GLUT doesn't allow you to specify user defined callback data. You could use the ffcall library to create in-situ closures which are then passed to GLUT. But there's the following to consider: As soon as you hit this wall, it's time to ditch GLUT. GLUT is not a requirement for OpenGL development and was never meant as a base for complex applications. So just don't use it then.

这篇关于如何在全局范围内初始化struct/class的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆