3

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

3
  • 2
    try moving the assignment to the main. There is nothing special in including GL/glut.h - the error you are observing has nothing to do with it. Commented May 10, 2012 at 7:57
  • 1
    Thanks a lot....would you mind explain me why we can not do in this way? The memory already assign to object a, why can not we modified it? And e.eg. if we claim a vector<int> M, outside main, we might push_back several element into M outside the main. Any difference between these two? Commented May 10, 2012 at 8:27
  • @mitweyl: Because the order of operations wouldn't be defined then. Does the assignment happen before or after calling the constructure (in the case of C++). On the global scope only static initialization is allowed. Commented May 10, 2012 at 8:34

3 Answers 3

2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

The newer syntax is for c++11 as well, or only c99?
@BЈовић it will be in C++ 20
0

you are doing an assignment outside of any function. In your case you could move only one line of code to get the following:

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

Comments

0

The problem you are observing is not related to including glut. The compiler does not allow you to assign value to a structure in the global scope. You can achieve what you want either by calling a constructor of the structure(which would be allowed at global scope) or by calling the assignment in some function.

EDIT: here is a somewhat related discussion: Why can't I access my array subscript in global scope

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.