Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Johnny_Blaze93

Extern with Structs, PLEASE HELP!!!!

This topic is 5958 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

okay, im 13 yrs old, and im tryng to make a little RPG game with crappy graphics in DOS and i dont know whats wrong with this (this is just an example): //This is in characterinfo.cpp struct Character { int life; int mana; } Character player; //This is in game.cpp #include "stdafx.h" #include <iostream.h> #include <conio.h> #include <stdlib.h> #include <string.h> main() { cout <<"Life:"<< extern player.life; cout <<"Mana:"<< extern player.mana; } That doesnt work, and the compiler keeps giving me errors, any suggestions???

Share this post


Link to post
Share on other sites
Advertisement
The "extern" is the problem. You need to actually declare the variable "player". Extern is used when you have a variable declared somewhere else and need to tell other files about it. For example:

Header file:
...
extern Character player;
...

CPP file:
...
Character player;

player.life = 100;
cout << "Life: " << player.life;
...


Now, everything that includes the header file will know about the player character. But when you actually use that variable, don''t use the "extern" keyword. Also, you need to initialize your stuff. You might get lucky and have everything initialized to 0 at the start of the game, but don''t count on it. Hope this all helps!

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
also use iosteam, not iostream.h
however you''ll also have to add:
using namespace std;
to each file that includes headers without the .h, but you should use the ones without the .h whenever one exists. They are the official C++ versions, and aren''t always the same as the .h versions.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!