QJ.NET | Videos | Forums | iPhone | MMORPG | Nintendo DS | Wii | PlayStation 3 | PSP | Xbox 360 | PC | Downloads | Contact Us
Forums | Gaming News | Videos | Downloads | Today's Posts | Mark Forums Read | Chat | FAQ | Members List | Contact

QJ.net Game Discussion - PSP, Xbox, Wii, PS3, PSP Homebrew, and PSP Guides

Go Back   QJ.net Game Discussion - PSP, Xbox, Wii, PS3, PSP Homebrew, and PSP Guides > Developers Corner > PSP Development, Hacks, and Homebrew > PSP Development Forum
The above video goes away if you are a member and logged in, so log in now!

Sharing Variables in C++ from a main.h

This is a discussion on Sharing Variables in C++ from a main.h within the PSP Development Forum forums, part of the PSP Development, Hacks, and Homebrew category; I need some help! I converted rpg paradise from C to C++ and then I got 'multiple definition' errors. I ...

Closed Thread
 
LinkBack Thread Tools
Old 06-01-2007, 11:34 AM   #1
 
pspballer07's Avatar
 
Join Date: Feb 2007
Posts: 315
Trader Feedback: 0
Default Sharing Variables in C++ from a main.h

I need some help! I converted rpg paradise from C to C++ and then I got 'multiple definition' errors. I tried changing the vars to 'static' vars but then they don't share the var, they just each get their own of that var.

So how do I declare a var that will be shared amoung different c++ files from a header file? So say main.h had this
Code:
int i;
and main.cpp had this somewhere:
Code:
i = 211;
if(i == GetVar()){ printf("This is what I want."); }
and another C++ file had this:
Code:
int GetVar()
{
        return i;
}
and both C++ files included the main.h
-= Double Post =-
I found the solution, I had to use extern, mods can delete this thread

Last edited by pspballer07; 06-01-2007 at 11:59 AM.. Reason: Automerged Doublepost
pspballer07 is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Old 06-01-2007, 12:35 PM   #2
 
PSPJunkie_'s Avatar
 
Join Date: Dec 2006
Location: main();
Posts: 1,071
Trader Feedback: 0
Default

Code:
extern i = 211;
if(i == GetVar()){ printf("This is what I want."); }
__________________
YEAH!

[center][COLOR="DimGray"][SIZE="2"][FONT="Times New Roman"]Julius Parishy
[[URL="http://jparishy.com"]Homepage[/URL]][/FONT][/COLOR]

Affiliates:
[url=http://xfacter.jparishy.com/]Xfacter[/url] | [url=http://flatmush.jparishy.com/]Flatmush[/url] | [url=http://yaustar.jparishy.com/]Yaustar[/url]

[url=http://jparishy.com/wp/source-code/]Soure Code[/url]

[/center]
[/size]
PSPJunkie_ is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Old 06-01-2007, 02:32 PM   #3

Heroes never die
 
hallo007's Avatar
 
Join Date: Aug 2006
Location: ...........
Posts: 1,318
Trader Feedback: 0
Default

Could you give one reason why you dont ask help in the c/c++ help thread???
hallo007 is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Old 06-01-2007, 02:38 PM   #4
 
PSPJunkie_'s Avatar
 
Join Date: Dec 2006
Location: main();
Posts: 1,071
Trader Feedback: 0
Default

**** off! Stop antagonizing every single person who asks a question!

Why do you make flashers?
Do you know you are hated by the entire PSP-Programming community?
Do you honestly think you have any right to ask a question?
What makes you better than anyone else?
Could your damn ego be any bigger?

Instead of being an ******* and repeating yourself to the same people with the same question, why don't you go ahead and answer those questions for me?

Here, I'll give you a hint on one of them. No one gives a **** what you think or what you say, so you have no right to be saying anything to this kid.

I'm telling you right now, we have been wanting to do this for a very long time. We don't want you here.

Stop acting like you're the boss.
__________________
YEAH!

[center][COLOR="DimGray"][SIZE="2"][FONT="Times New Roman"]Julius Parishy
[[URL="http://jparishy.com"]Homepage[/URL]][/FONT][/COLOR]

Affiliates:
[url=http://xfacter.jparishy.com/]Xfacter[/url] | [url=http://flatmush.jparishy.com/]Flatmush[/url] | [url=http://yaustar.jparishy.com/]Yaustar[/url]

[url=http://jparishy.com/wp/source-code/]Soure Code[/url]

[/center]
[/size]
PSPJunkie_ is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Old 06-01-2007, 03:30 PM   #5

...in a dream...
 
SG57's Avatar
 
Join Date: Jul 2005
Posts: 4,957
Trader Feedback: 0
Default

Quote:
Originally Posted by PSPJunkie_
**** off! Stop antagonizing every single person who asks a question!

Why do you make flashers?
Do you know you are hated by the entire PSP-Programming community?
Do you honestly think you have any right to ask a question?
What makes you better than anyone else?
Could your damn ego be any bigger?

Instead of being an ******* and repeating yourself to the same people with the same question, why don't you go ahead and answer those questions for me?

Here, I'll give you a hint on one of them. No one gives a **** what you think or what you say, so you have no right to be saying anything to this kid.

I'm telling you right now, we have been wanting to do this for a very long time. We don't want you here.

Stop acting like you're the boss.
Ditto

Anyways, external variables aren't C++ specific, so going from C to C++ shouldn't have changed anything, nor been necessary.

Usual practice is to put:

Code:
extern int foo;
into foo.h, and put:

Code:
#include "foo.h"
int foo = 0;
into foo.c, and put:

Code:
#include "foo.h"
into all the other .c files that need to access the variable "foo".
Including foo.h in foo.c allows the compiler to check the declaration in
foo.h against the corresponding definition in foo.c to ensure that they
match.
__________________
SG57 is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Old 06-02-2007, 01:19 AM   #6
 
M7thCC's Avatar
 
Join Date: Sep 2005
Posts: 2,223
Trader Feedback: 0
Default

Closed, keep the fire a bit lower next time, alright guys?
M7thCC is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Closed Thread

Tags
mainh , sharing , variables

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off



All times are GMT -8. The time now is 03:19 AM.



Use of this Web site constitutes acceptance of the TERMS & CONDITIONS and PRIVACY POLICY
Copyright © 2009, QJ.NET. All Rights Reserved.
Contact Us