ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I have one sourcefile with main in it that declares a class-object. Now after doing some stuff the main-function calls to another function that is defined in another document.
I now want the second function to get access to the class-object. I have tried using the word "extern" but it seems like I have to define it outside of any function then, but this class-object must be defined within the main-function and I have several more files after this, all should be able to access the class-object so sending it as argument or such would not be as easy as just making it external and aviliable for all other files.
Originally posted by btmiller Put the class definition into a header file and include it in all the files that need to know the class definition.
Yeah that was not the problem. The files knew about the class but the object created in main was not external. I could not figure out any other way to do it but create the object outside of main and move all code from the constructor into a init-function. It worked, though.
Sorry, I misread your question. IIRC there's no way around your situation -- extern variables must be declared at file scope in the file that contains their real definition. This sort of makes sense, given that these variables might go out of scope otherwise. Can you simply pass the object (or a reference to it) to all the functions that need to use it? That would be the best way, IMO and avoid the excessive use of globals.
I mostly use C, though, so I'm not aware if there are any special C++ features that would modify this answer (though I tinkered around with a test program a little). I'm also not 100% certain of the scoping rules (bbeen some time since I had to deal with them), so someone please correct me if I've got this wrong (I'm curious myself now), but the answer I gave above is how I remember it.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.