1

In my code I have two functions, Save and Load file, which operate on an array called Players. The array Players is defined in a second file, along with some other variables. Lastly, I have a main file with the main function.

In the main function, I call Load, which writes player names from a file into the array Players. When I then attempt to output the Players with cout, all of them are empty, even though between the call to Load and the cout statements in main, I do nothing.

When I print Players in the Load function, the names are printed as expected. So, seems that the contents of Players gets deleted when I use it in the main function.

The Load Function:

void Load() {
    std::string line;

    std::ifstream LoadStream;
    LoadStream.open(PlayersFilePath);

    if (LoadStream.is_open()) {
        for (int i = 0; i < MaxPlayers; i++) {
            std::getline(LoadStream, line);
            Players[i] = line;
        }

        return;
    }
    else {
        std::cout << Error1 << PlayersFilePath << std::endl;
        exit(0);
    }
}

Here is the Variable File with the Players Array:

#include <string>

using namespace std;

static const int MaxPlayers = 10;
static int ActivePlayers;
static int ActivePlayerIndex1, ActivePlayerIndex2, ActivePlayerIndex3, ActivePlayerIndex4, ActivePlayerIndex5, ActivePlayerIndex6, ActivePlayerIndex7, ActivePlayerIndex8, ActivePlayerIndex9, ActivePlayerIndex10;
static int counter;

static const string Error1 = "File could not be found or opened. File: ";
static const string Error2 = "Saving Error";
static const string PlayersFilePath = "C:\\Users\\Felix\\source\\repos\\ClassTraining\\Players.txt";

static string Players[MaxPlayers];

static const string German = "Deutsch";

static const string English = "English";

And here is the Main Function:

#include <iostream>
#include <string>
#include "Variables.cpp"
#include "SaveLoad.h"

int main() {
    Load();

    for (int i = 0; i < MaxPlayers; i++) {
        std::cout << Players[i] << std::endl;
    }

    return 0;
}
Brian61354270
  • 8,690
  • 4
  • 21
  • 43
Bugsia
  • 21
  • 1
  • 5
  • 3
    Please add a [Minimal, Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example) – Geno C Aug 09 '20 at 20:58
  • Hello! I advise you to check out [How to ask a good question](https://stackoverflow.com/help/how-to-ask) and [How to create a minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example). – Brian61354270 Aug 09 '20 at 20:58
  • 1
    Please show us how `Players` (assuming this is the array you're talking about) is declared in both files. – Brian61354270 Aug 09 '20 at 21:01
  • You should use a vector instead of array. – Barmar Aug 09 '20 at 21:05
  • 1
    `static string Players[MaxPlayers];` This defines `Players` as a *different* `static` array in each file that includes the header. To share the *same* array between files (technically: translation units), change `static` to `extern` declaration in the header file, then add a definition `string Players[MaxPlayers];` in exactly *one* source file. The same applies to the other variables as well. – dxiv Aug 09 '20 at 21:08
  • Do you know what [internal linkage](https://stackoverflow.com/questions/1358400/what-is-external-linkage-and-internal-linkage) is? – Brian61354270 Aug 09 '20 at 21:10
  • Okay. I now changed it to extern, but now I get the error LNK2001 – Bugsia Aug 09 '20 at 21:16
  • @Bugsia As dxiv noted, make sure you are only _defining_ `Players` in one translation unit (i.e., source file). If you haven't already, try splitting `Variables.cpp` into a header and implementation file. The header should include `extern std::string Players[MaxPlayers];`, while the implementation file should include `std::string Players[MaxPlayers];` without the `extern` specifier. Also, you should try to [avoid including `using namespace std`](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice) in a header file. – Brian61354270 Aug 09 '20 at 21:21
  • 1
    Does this answer your question? [Access static variable from CPP file to other header file](https://stackoverflow.com/questions/52025196/access-static-variable-from-cpp-file-to-other-header-file) – JaMiT Aug 09 '20 at 21:21
  • @Brian I doesnt really undersrand it. when I doesnt include string in the heade file, then i cant use std::string. And should i do it the same way with the rest of the variables? – Bugsia Aug 09 '20 at 21:36
  • @Bugsia If you haven't already, read through the top answer of the proposed duplicate. It seems that you may misunderstand either how header files work or what linkage is. Every variable you define in a header file with be duplicated as _different_ variables in each source file that includes the header. If you want to use the same variable in more than one source file, you need to define it in precisely one file and then declare with as as symbol with external linkage (with `extern`) in the remaining files. – Brian61354270 Aug 09 '20 at 21:44
  • Thank you all. Now its working – Bugsia Aug 09 '20 at 22:38
  • Even though it might be working now, you really should ditch every single (non-const) global variable and use proper classes instead. – Aziuth Aug 10 '20 at 07:53
  • @Aziuth Okay, but how do I do that? I would need the same instance of the class in multiple Files and I dont know how to share them – Bugsia Aug 10 '20 at 11:03

0 Answers0