Search code examples
c++classheadergetlineextern

I'm getting an error when I try to use extern to have variables across multiple source files


EDIT: I forgot to use the std namespace. That fixed some errors but not all.

I'm making a text-based game. Originally, the game was in one, main source file. Now, for ease of navigation (the game is open-sourced, and I'm trying to encourage modding), I want to split it up into several files. Naturally, to do this, I need variables that work across all of the files.

So, I took all of my global variables, moved them to a header file, and slapped extern on them. Yes, I defined them in the main source file without extern.

Here's what the header file with the definitions looks like:

#include <string>

extern string start; //Starts the game.
extern int scenario; //Holds the random number that decides the scenario that plays.
extern int roomScenario; // Holds the random number that decides what room scenario plays.
extern int isRandom = 1; //Holds whether the map is randomized. On by default.
extern int isSeedOn;//Holds whether the user can input a seed.
extern int isCodes = 1; //Holds whether the codes system is on. On by default. 
extern int mapSeed; //The seed that the map creator uses.
extern int cD; //The current intersection the player is at. 0-15.
extern int deaths = 0; //The number of time the player has died. Zero by default.
extern int length = 16; //Length of catacombs. 16 by default.
extern int eLength = 15; //Effective length. Used to calculate whether the user is at the ladder.
extern int totalLives; //Total number of lives.
extern int lives; //Number of lives. Length / 2.
extern int livesRemain; //Number of lives remaining. It's value is equal to 'lives' - 'deaths'.
extern int lastDiedOn; //Hold the intersection the player last died at. Will be used to leave the sketchpad to the next player.
extern bool msgRead; //Hold whether the player has read the killer's message. Determines which ending you get.
extern bool takenSheet; //Stuff with fore/farsight
extern string ladderDecision; //The decision of the player when they're at the ladder.
extern string option; //Holds what the player chooses in the options menu.

Here are some of the errors I'm getting:

On all of the string variables, I'm getting these two errors:

Error C2146: syntax error : missing ';' before identifier '[VARIABLE_NAME]'

Error C4430: missing type specifier - int assumed.

Does extern not work with string variables? If not, what would be an alternative?

On all of the string variables that I define, (not shown in above header file segment) I get this error as well:

error C2440: 'initializing' : cannot convert from 'const char [VARIABLE_SIZE]' to 'int' 

Again, does extern not work with string variables?

Further down in my header file, I have three classes, and some arrays.

class intersect{
public:
    string solve; //What the correct path is.
    bool isCoffin; //Holds if the room is Scenario_2.
    bool isSparkle; //Holds if the room is Scenario_1.
};
class foresight{
public:
    string msg; //The message of this code.
    string msg2; //The second message.
    int intCalled; //What intersection the code was found at. (Both of the codes would be found at the same intersection)
    bool isCalled; //If the the code has been found.
    bool isCalled2; //If the second code has been found.
};
class farsight{
public:
    string msg;
    string msg2;
    int intCalled;   //All of these have the same purpose as the ones of the same name in foresight. Look there for the documentation.
    bool isCalled;
    bool isCalled2;
};
extern intersect int1, int2, int3, int4, int5, int6, int7, int8, int9, int10, int11, int12, int13, int14, int15, int16, int17, int18, int19, int20, int21, int22, int23, int24, int25, int26, int27, int28, int29, int30, int31, int32;
extern foresight fore1, fore2, fore3, fore4, fore5, fore6, fore7, fore8, fore9, fore10, fore11, fore12, fore13, fore14, fore15, fore16, fore17, fore18, fore19, fore20, fore21, fore22, fore23, fore24, fore25, fore26, fore27, fore28, fore29, fore30, fore31, fore32;
extern farsight far1, far2, far3, far4, far5, far6, far7, far8, far9, far10, far11, far12, far13, far14, far15, far16, far17, far18, far19, far20, far21, far22, far23, far24, far25, far26, far27, far28, far29, far30, far31, far32;
extern intersect ints[32] = { int1, int2, int3, int4, int5, int6, int7, int8, int9, int10, int11, int12, int13, int14, int15, int16, int17, int18, int19, int20, int21, int22, int23, int24, int25, int26, int27, int28, int29, int30, int31, int32 };
extern string msgs[32] = { msg1, msg2, msg3, msg4, msg5, msg6, msg7, msg8, msg9, msg10, msg11, msg12, msg13, msg14, msg15, msg16, msg17, msg18, msg19, msg20, msg21, msg22, msg23, msg24, msg25, msg26, msg27, msg28, msg29, msg30, msg31, msg32 };
extern foresight fore[32] = { fore1, fore2, fore3, fore4, fore5, fore6, fore7, fore8, fore9, fore10, fore11, fore12, fore13, fore14, fore15, fore16, fore17, fore18, fore19, fore20, fore21, fore22, fore23, fore24, fore25, fore26, fore27, fore28, fore29, fore30, fore31, fore32 };
extern farsight fars[32] = { far1, far2, far3, far4, far5, far6, far7, far8, far9, far10, far11, far12, far13, far14, far15, far16, far17, far18, far19, far20, far21, far22, far23, far24, far25, far26, far27, far28, far29, far30, far31, far32 };
extern string names[16] = { first1, first2, first3, first4, first5, first6, first7, first8, first9, first10, first11, first12, first13, first14, first15, first16 };

Each of the variables in the ints[](Using the 32 intersect variables declared above) array, the fore[] (Using the 32 foresight variables declared above) array, and the fars[] (Using the 32 farsight variables declared above) array generate an error. The error says that the variables in the arrays are re-declaring the variables declared above, simply be them being in an array. Funny thing is, though...the original declarations have the same error. Hilarious, right?

And finally, the last of the errors in the header file. These appear when I put some(but not all, for some strange reason) of the intersect variables into the ints[] array.

On int1 and int4, I get the error:

IntelliSense: no suitable user-defined conversion from "intersect" to "std::string" exists

On int2 and int3, I get the error:

IntelliSense: no suitable conversion function from "intersect" to "bool" exists

This has to do with the data members, right?

Anyway, in the source file, I get even more errors! Well, of only one type.

Everytime I use getline(), I get this error:

IntelliSense: no instance of overloaded function "getline" matches the argument list
        argument types are: (std::istream, <error-type>)

I would use getline() like this:

getline(cin, start)

What's going on here?

Thankfully, that's all of them, but it's still a heck of a lot. (Over 400 in total!) Any help would be greatly appreciated!


Solution

  • You cannot define variable which is declared asextern. Extern only declares variable, saying it exists, not defines it. The variable should be defined in different place. So extern int x=1; is wrong.

    Header:

    extern int x;
    

    Somewhere in other place(probably other file):

     int x = 1;
    

    or

    int x;
    x = SomeFunction();