Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to initialize struct/class on the global scope

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

The following is a sample code:

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

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?

Thanks a lot

like image 494
mitweyl Avatar asked Aug 13 '26 08:08

mitweyl


1 Answers

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

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};

or using a newer syntax

struct A a = {.x = 6};

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

Update/EDIT:

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

Update/Edit 2

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

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.

like image 137
datenwolf Avatar answered Aug 14 '26 21:08

datenwolf



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!