Extern with Structs, PLEASE HELP!!!!

Started by
2 comments, last by Johnny_Blaze93 21 years, 9 months ago
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???
I OWN YOU
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!
yeah that was just example code i typed up in like 3 secs, not actually from my game... well thanks alot for the help!
I OWN YOU
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.

This topic is closed to new replies.

Advertisement