Register | Login
Views: 19364387
Main | Memberlist | Active users | ACS | Commons | Calendar | Online users
Ranks | FAQ | Color Chart | Photo album | IRC Chat
11-02-05 12:59 PM
Acmlm's Board - I2 Archive - - Posts by beneficii
Pages: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
User Post
(restricted)
beneficii

Lakitu
Level: 36

Posts: 382/567
EXP: 299656
For next: 8454

Since: 06-27-04
From: Cordova, TN, USA

Since last post: 14 hours
Last activity: 6 hours
Posted on 08-05-05 10:34 AM, in Where did you get your username from? Link
My username is simply the genitive form of the Latin noun beneficium (pronounced "beh neh fee kee oohm"), beneficii (pronounced "beh neh fee kee ee") (n) which means "good deed" or "kindness." My name is not in any way, shape, fashion, or form Ben and I resent people who call me Ben because it's just not me.

I've had beneficii for at about 3-4 years and was originally an RA2 Westwood Online account because I was trying to think of a new one to replace my old one that I got tired of.


(edited by beneficii on 08-05-05 01:35 AM)
(edited by beneficii on 08-05-05 01:37 AM)
(edited by beneficii on 08-05-05 01:37 AM)
(edited by beneficii on 08-05-05 01:39 AM)
(restricted)
beneficii

Lakitu
Level: 36

Posts: 384/567
EXP: 299656
For next: 8454

Since: 06-27-04
From: Cordova, TN, USA

Since last post: 14 hours
Last activity: 6 hours
Posted on 08-06-05 10:31 AM, in C++: Bizarre Multiple Declarations Link
Alright, I'm working on a project in C++ that has multiple source files with the same header file between them. Any variable I declare specific to a source file I declare as static (i.e. that is only for that source file) and any variable that is shared between source files is declared in the header file (not as static) I would think that most here would agree that this is standard practice in really big projects.

Now, I've tried compiling my project, I've gone through and corrected all syntax editors and it looks like I'm finally going to get it to compile when BAM! a very looooong list of linker errors shows up. They all said:

multiple definition of 'variable'
first defined here

(I'm using Bloodshed Dev C++ v. 4.9.9.2 btw and its default compiler GNU C++ Compiler.)

After that, I look at those variables and I see that they are all declared in the header file! I go to the compile log and see basically this:

sourcefileB.cpp: multiple definition of 'variableJ' // again variableJ is declared only in the header file
sourcefileA.cpp: first defined here
sourcefileC.cpp: multiple definition of 'variableJ'
sourcefileA.cpp: first defined here
sourcefileD.cpp: multiple definition of 'variableJ'
sourcefileA.cpp: first defined here

I'm thinking, okay, why is there a conflict here? Doesn't the linker know that these variables were all declared in a header file that was simply used by all the source files? I wrote a much smaller program afterward with two source files and a header file, which was included in both source files. I declared some variables in the header file. When it compiled, there was no conflict and the program ran normally. I'm wondering, What makes my program so different? Is it a problem with the compiler, or is there perhaps a common trap that I'm falling for?

Can any of you perhaps help me? Thanks.
beneficii

Lakitu
Level: 36

Posts: 385/567
EXP: 299656
For next: 8454

Since: 06-27-04
From: Cordova, TN, USA

Since last post: 14 hours
Last activity: 6 hours
Posted on 08-06-05 10:50 AM, in Try And Beat This Guy! Link
I won. Basically, you want it to be your opponent's turn when there are 5 gemstones left. If it's his turn, then there is no way he can win if you're playing your best. Now about getting down to that....

Edit: If it's your turn and you have 9 left, you also lose.

Edit 2: Ah, I figured it out now. It's actually quite simple, you want to avoid having it be a "losing" number when it's your turn. The losing numbers start at 5 and increment by 4 each time. That is why you want to go first, because you want to get your opponent with the losing numbers from the start otherwise you can't get out of him doing that to you.


(edited by beneficii on 08-06-05 01:52 AM)
(edited by beneficii on 08-06-05 01:54 AM)
(edited by beneficii on 08-06-05 01:58 AM)
(edited by beneficii on 08-06-05 01:59 AM)
(edited by beneficii on 08-06-05 02:02 AM)
beneficii

Lakitu
Level: 36

Posts: 386/567
EXP: 299656
For next: 8454

Since: 06-27-04
From: Cordova, TN, USA

Since last post: 14 hours
Last activity: 6 hours
Posted on 08-06-05 11:34 AM, in C++: Bizarre Multiple Declarations Link
Originally posted by neotransotaku
try placing this as the first two lines of the shared header file

#ifdef __headerfile
#define __headerfile

and this as the end of the shared header file

#endif


usually this is the techinique to ensure a header file is included only once


Hold on.

Nope, if I do it #ifdef __headerfile, then __headerfile will never get defined. I tested it out anyway, and when I compiled the program it said that none of the variables defined in the header file were defined. I next tried #ifndef __headerfile, which seemed more logical, but still had the problem of multiple declarations. I counted the same number of underscores and everything. I wonder what's wrong.


(edited by beneficii on 08-06-05 02:37 AM)
(edited by beneficii on 08-06-05 02:43 AM)
beneficii

Lakitu
Level: 36

Posts: 387/567
EXP: 299656
For next: 8454

Since: 06-27-04
From: Cordova, TN, USA

Since last post: 14 hours
Last activity: 6 hours
Posted on 08-06-05 08:46 PM, in C++: Bizarre Multiple Declarations Link
Originally posted by Disch
If you define a variable to be global, it will have global scope (Global meaning it exists for the whole program). If you #include a header file which has a var declaration in several .cpp files, each .cpp file will create its own variable with the same name. This is where your problems are coming from.

The solution here is to make the variable once, then link it so that all .cpp files have access to it.


int myVar; /* will actually make the variable -- only have 1 of these */

extern int myVar; /* will not make the variable, will link to it after compile time -- have one of these for each .cpp file beyond the first */


Of course extern int myVar will only work if 'int myVar' is declared somewhere in your program.

For a halfway decent analogy of why it works this way... making the actual variable (int myVar) actaully makes the variable with a "body". Similar to how making a function with a body gives the function it's body. If you have a function in your program which has several bodies, you'll get errors much like the ones you were getting -- because you were making several different variables which have the same body -- and the compiler doesn't know which one you want to use.

Making it 'extern' says "okay, this variable exists... but not in this .cpp file, so we'll link to it, but it won't be in this compile" -- which is kind of like a body-less variable. Equivilent to having a function prototype (function without a body). It's making the function/var available for use, but without actually making it. Of course if you actually call the function, it will need to have a function body found at link time. Likewise if you actually use an extern var, it will need to find a var body at link time. Otherwise you get errors.


So yeah, only 1 cpp file has the variable. The others use 'extern' to link to that var. Yeah it's a pain in the ass -- which is one of the reasons why I avoid global vars like the plague.

EDIT:

To avoid a pain in the ass, you can sort of cheat with clever use of #defines



// your header file (referred to as "include.h" below)

#ifdef DO_NOT_MAKE_EXTERN
#define _EXTERN
#else
#define _EXTERN extern
#endif


_EXTERN int Var1;
_EXTERN int Var2;
//etc




// in **ONE** of your cpp files
#define DO_NOT_MAKE_EXTERNS
#include "include.h"

// rest of file here




// in all the other cpp files

#include "include.h" // note, no #define here

//rest of file here




That'll make it easier to manage a large file full of global vars. The #define _EXTERN saves you from having to make (and maintain) two sets (bodied and body-less) since it keeps both in the same file.


Okay, so I would have to add _EXTERN in front of every variable in the header file? Also, put in DO_NOT_MAKE_EXTERN and DO_NOT_MAKE_EXTERNS or was that a typo? Thanks.
beneficii

Lakitu
Level: 36

Posts: 388/567
EXP: 299656
For next: 8454

Since: 06-27-04
From: Cordova, TN, USA

Since last post: 14 hours
Last activity: 6 hours
Posted on 08-06-05 08:55 PM, in C++: Bizarre Multiple Declarations Link
Originally posted by Disch
Originally posted by beneficii

Okay, so I would have to add _EXTERN in front of every variable in the header file?


Yes. That will make it bodied or body-less depending on whether or not DO_NOT_MAKE_EXTERNS was defined before including the header.


Also, put in DO_NOT_MAKE_EXTERN and DO_NOT_MAKE_EXTERNS or was that a typo? Thanks.


sorry, that was a typo. It does matter what it is as long as it's the same thing both times. *me goes to edit post to correct that*


I think I understand what this does. If DO_NOT_MAKE_EXTERN is defined, then it defines __EXTERN as nothing, which makes the variable normal. If it's not defined, then it defines __EXTERN as extern, which makes the variable extern. You only want it one source without the extern, while everyone else should have it, so you define DO_NOT_MAKE_EXTERN for it. Thanks.

EDIT: Ugh this is so frustrating! I did as you said, and it did something new. First, it said:

[Warning] 'vars' initialized and declared 'extern' // for each variable in the header file

then it went right back to complaining about multiple declarations as it did before. I declared DO_NOT_MAKE_EXTERN in one file (and it was for that file ("in file included from file") I didn't get the warnings). Do you have any recommendations?


(edited by beneficii on 08-06-05 12:18 PM)
beneficii

Lakitu
Level: 36

Posts: 389/567
EXP: 299656
For next: 8454

Since: 06-27-04
From: Cordova, TN, USA

Since last post: 14 hours
Last activity: 6 hours
Posted on 08-06-05 09:55 PM, in C++: Bizarre Multiple Declarations Link
Originally posted by Disch
don't init the var when it's declared.

int myvar = 5; <--- don't do that, it won't like it when the var is extern

Instead init all your vars at program startup -- like call an Init() function or something which sets everything to what you want it to be.


Uh, well, that's going to take some time to correct, because those vars are meant to be constant arrays, but I'll see what I can do.
beneficii

Lakitu
Level: 36

Posts: 390/567
EXP: 299656
For next: 8454

Since: 06-27-04
From: Cordova, TN, USA

Since last post: 14 hours
Last activity: 6 hours
Posted on 08-06-05 10:10 PM, in C++: Bizarre Multiple Declarations Link
Originally posted by Disch
If they're constant then declare them as constant and then you don't have to worry about that extern crap:

const int myArray[5] = {0,1,2,3,4};

that should work fine. If you still get problems (which I doubt), try making it static as well:

static const int myArray[5] = {0,1,2,3,4};


Okay, thank you! I should have seen that! If a variable is initialized in the declaraqtion it won't let it be extern.

Thank you! It now compiles succesfully.

Onto the next stage of my program and arguably the most challenging: the logic tests!
beneficii

Lakitu
Level: 36

Posts: 391/567
EXP: 299656
For next: 8454

Since: 06-27-04
From: Cordova, TN, USA

Since last post: 14 hours
Last activity: 6 hours
Posted on 08-06-05 11:11 PM, in What would the world be like... Link
It'd be a useless mathematical system and we'd all be dummies for implementing it, so yeah the caves live in we would.
beneficii

Lakitu
Level: 36

Posts: 392/567
EXP: 299656
For next: 8454

Since: 06-27-04
From: Cordova, TN, USA

Since last post: 14 hours
Last activity: 6 hours
Posted on 08-07-05 02:32 AM, in Try And Beat This Guy! Link
Originally posted by Kefka
Yea, actually, your opponent is dead not at 5, but at 13. So if you get them at 13, then you've won if you keep doing 3. However, I'm not gonna bother putting a spoiler tag on this, cause I'm sure everyone has read the other ones already.

There IS a way I have found to winning when taking a second turn... Play with that for a while, eh?

I bet people actually believe this and will try it now xD


You can't win when you pick the second turn: not if your opponent is playing his best. I think that the computer you go against does play its best.
beneficii

Lakitu
Level: 36

Posts: 393/567
EXP: 299656
For next: 8454

Since: 06-27-04
From: Cordova, TN, USA

Since last post: 14 hours
Last activity: 6 hours
Posted on 08-07-05 03:57 AM, in Question About Static Variable--Going Out of Scope Link
When do static variables (declared in a particular source file) go out of scope? It seems like when a DlgProc is called in a source file the static variables in that source file go out of scope because all of a sudden they get weird random values (making me think that they are as though they haven't been initialized). So what's causing it?
beneficii

Lakitu
Level: 36

Posts: 394/567
EXP: 299656
For next: 8454

Since: 06-27-04
From: Cordova, TN, USA

Since last post: 14 hours
Last activity: 6 hours
Posted on 08-07-05 04:22 AM, in Question About Static Variable--Going Out of Scope Link
Originally posted by Disch
Static variables maintain "normal" scope in the sense that if you declare them within a function, they can only be used inside that function. Or when declared in the class, they can only be used in the class (or by YourClass::varname).

However, if you're talking about when the variable is created/destroyed -- static variables have global scope in that sense -- they're created once on program startup and destroyed on program exit. They maintain their value through any function calls or any scope changes, as long as the running instance remains in tact.

For example:




int CountUp(void)
{
static int count = 0;
count++;
return count;
}




Calling that function will return 1 the first time it's called, then 2 the next time, then 3, and so on. Whereas if 'count' were non-static, you'd get 1 every time.


Static class member variables follow in the same vein. If you declare a static variable within a class, only one variable is created (regardless of the number of class objects created) and EVERY OBJECT of that class type will share that same variable. Likewise if the variable is public -- 'outsiders' to the class can access that variable with 'YourClassName::thevarname' -- you don't even need an object of the class to access it -- since it doesn't rely on any.


What's been happening is this:

#include "myfavoriteheaderfile.h"

static int var1; //outside of function

int getbox(HWND owner) {
var1 = 5;
return DialogBox((HINSTANCE) GetModuleHandle(NULL), MAKEINTRESOURCE(IDD_MOTHERDUDDER), owner, MyFavProc);
}

BOOL CALLBACK MyFavProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam) {
switch(msg) {
case WM_INITDIALOG:{
char tempchr[10];
itoa(var1, tempchr, 10);
MessageBox(hwnd, tempchr, "Titlebar", MB_OK);
/* In the body of the MessageBox, it will show some weird ass value--every value I've seen is always "80808080" */
/* etc etc rest of program */

Do you seem my point, Disch? It's like var1 went out of scope, because it gave the value of 80808080. What do you recommend for that?


(edited by beneficii on 08-06-05 07:24 PM)
beneficii

Lakitu
Level: 36

Posts: 395/567
EXP: 299656
For next: 8454

Since: 06-27-04
From: Cordova, TN, USA

Since last post: 14 hours
Last activity: 6 hours
Posted on 08-07-05 04:53 AM, in Question About Static Variable--Going Out of Scope Link
Originally posted by Disch
I just tried that code and got it to print 5. Don't know what to tell ya.

Any paticular reason why you need this var to be static anyway? I mean it's already global....


That was just a sample. I thought you would have noticed that wasn't my real program. Anyway, I had the WndProc call a function and it didn't return 80808080 anymore, because like totally that is messed up. Still, somtimes when I call it function from the Proc it'll still give me 80808080. And I get all sorts of unpredictable effects from that.
beneficii

Lakitu
Level: 36

Posts: 396/567
EXP: 299656
For next: 8454

Since: 06-27-04
From: Cordova, TN, USA

Since last post: 14 hours
Last activity: 6 hours
Posted on 08-07-05 05:31 AM, in What the Hell's Going On? Link
This just spooks me out:

http://sun-herald.com/Newsheadline.cfm?headline=6413&banner=1

There was a huge convoy of sea creatures heading south near Englewood Beach in Flordia and freaky they weren't attacking each other or anything. Something seems so foreboding about all this....
beneficii

Lakitu
Level: 36

Posts: 397/567
EXP: 299656
For next: 8454

Since: 06-27-04
From: Cordova, TN, USA

Since last post: 14 hours
Last activity: 6 hours
Posted on 08-07-05 08:51 PM, in What the Hell's Going On? Link
In greatest probability, red tide (overgrowth of algae causing depletion of oxygen in the water) was the cause of this, forcing the animals to move to another location or die. If so, then this is probably one of the severist cases of red tide on record.
beneficii

Lakitu
Level: 36

Posts: 398/567
EXP: 299656
For next: 8454

Since: 06-27-04
From: Cordova, TN, USA

Since last post: 14 hours
Last activity: 6 hours
Posted on 08-08-05 12:25 AM, in Hear ye! Hear ye! Link
Rebel! Freedom! Liberty! To arms! To arms!
beneficii

Lakitu
Level: 36

Posts: 399/567
EXP: 299656
For next: 8454

Since: 06-27-04
From: Cordova, TN, USA

Since last post: 14 hours
Last activity: 6 hours
Posted on 08-09-05 09:18 PM, in NASA Goes Up Again: Discovery Touches Down Link
Originally posted by Kasumi-Astra
Discovery safely touched down in California about one and a half hours ago, in a perfect landing. The rest of the fleet is still indefinately grounded, although NASA has not officially declared Atlantis' next mission to be canceled. If the issue of debris falling from any part of the shuttle during lift-off can be solved, Atlantis is due to begin a mission in September. Discovery's next flight is due to begin in April 2006.


I'm glad they managed to get home okay.
(restricted)
Pages: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
Acmlm's Board - I2 Archive - - Posts by beneficii


ABII


AcmlmBoard vl.ol (11-01-05)
© 2000-2005 Acmlm, Emuz, et al



Page rendered in 0.038 seconds.