Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Dark GDK / Classes and global variables

Author
Message
CheatCat
17
Years of Service
User Offline
Joined: 7th Mar 2007
Location: Sweden
Posted: 3rd Mar 2008 19:39 Edited at: 3rd Mar 2008 19:43
I wonder why this code don't work:

Main.cpp:


lol.h:


lol.cpp


And I get this error when I try to run:
Lilith
16
Years of Service
User Offline
Joined: 12th Feb 2008
Location: Dallas, TX
Posted: 3rd Mar 2008 20:07
Indication is that the integer "digit" is already define in main. I think what's happening here is that you have the directive

#include "lol.h"

in both your main.cpp and in lol.cpp. This essentially says you're code in lol.h is duplicated in both places. You need to get in the habit of insuring that this doesn't happen with your header files. What you want to do in your lol.h file is the following: (in spite of your #pragma???)



This tells the program that each time it sees that code it will test for _LOL_H (or whatever unique identifier you want) to be defined. If it isn't it will define everything up to the matching #endif, which matches the #ifndef (if not defined). On subsequent encounters the precompiler will see that _LOL_H is defined and thus will ignore everything up to the #endif.

I can't imagine why your pragma isn't working though. I'll have to test it on some of my existing code.

Lilith, Night Butterfly
Zotoaster
19
Years of Service
User Offline
Joined: 20th Dec 2004
Location: Scotland
Posted: 3rd Mar 2008 20:33
You can't initialize variables unless they are in a function.

Don't you just hate that Zotoaster guy?
Benjamin
21
Years of Service
User Offline
Joined: 24th Nov 2002
Location: France
Posted: 3rd Mar 2008 21:22 Edited at: 3rd Mar 2008 21:23
Quote: "in both your main.cpp and in lol.cpp. This essentially says you're code in lol.h is duplicated in both places. You need to get in the habit of insuring that this doesn't happen with your header files."

This isn't the problem. Header guards will make sure a header isn't included multiple times in the same object file (source file), but it can be included at least once in each source file. The problem is that the variable is being defined in multiple source files, which is not allowed.

Variables should never be defined in header files, only in source files. You may declare variables in header files using the extern keyword, which basically means "this variable is defined in another file".

What you need to do is put this in the header file:



And put this in a relevant source file:


Quote: "You can't initialize variables unless they are in a function."

You can initialize them in the global scope too.

Lilith
16
Years of Service
User Offline
Joined: 12th Feb 2008
Location: Dallas, TX
Posted: 3rd Mar 2008 22:13 Edited at: 3rd Mar 2008 22:14
Quote: "You can't initialize variables unless they are in a function."


Sure you can. A global variable can be assigned an intial value when it's declared. That's essentially what is happening when the header with the variable declaration is placed in global space.

Lilith, Night Butterfly
CheatCat
17
Years of Service
User Offline
Joined: 7th Mar 2007
Location: Sweden
Posted: 5th Mar 2008 09:34
Thanks!

Login to post a reply

Server time is: 2024-10-08 06:29:04
Your offset time is: 2024-10-08 06:29:04