0

I have ran into a problem yesterday when trying to split my code into several files.

Yesterday morning my whole code was in one file and to keep track of everything more easily I wanted to split the code into more files.

This went well until I got to a function where I need to declare a variable although I already have (but maybe in the wrong place).

Because the code is too long, I have put all files to pastebin.

I have declared "field" in main.cpp:

char field[20][41];

Whole file here: https://pastebin.com/Jy1XvdpL

And I want to use this in my field.cpp:

void loadLevel(int levelnumber) {

// concatenate leven base with level number
std::string level = "level" + std::to_string(levelnumber) + ".txt";

// print field
// load the text file
std::ifstream file;
file.open(level);
char c;

// read line by line, character by character and store it in field
for (int i = 0; i < 20; i++) {
    for (int j = 0; j < 41; j++) {
        file.get(c);
        field[i][j] = c;
    }
}
file.close();

}

The field.h looks like this:

#ifndef field
#define field

#include <iostream>
#include <string>
#include <fstream>

void loadLevel(int levelnumber);
void drawField();

#endif // !field

The problem is that I do not know, where to define char field because I get an error if done in either of these files. So what do I need to do to get char field workin in field.cpp and therefore work in my main?

P.S. This is my first program in c++ and I am learning new things everyday. I appreciate any hints on how to do certain things better ^^

Kind Regards, Benjamin

  • 2
    First of all, you don't declare the `field` variable in your header file, so `field.cpp` source file will not know about it (unless you have a separate declaration in it). A bigger problem though: What do you think `#define field` does? Perhaps you should [get a couple of good beginners books to read](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list)? Especially if this is your very first C++ program, you probably do way to much to learn something effectively. Start *much* simpler. – Some programmer dude Sep 29 '17 at 06:37
  • And for future questions, please learn how to create a [Minimal, Complete, and Verifiable Example](http://stackoverflow.com/help/mcve). Links can go stale, making the question worthless. – Some programmer dude Sep 29 '17 at 06:38
  • @Someprogrammerdude I will think about the books. But if I declare it in my field.cpp my main doesn't know about it because it says that it isn't declared. If I declare it in both files, there will be a duplication. – TheEarthkin Sep 29 '17 at 06:39
  • @Someprogrammerdude This is my first post and I will do better next time! Thank you for your help with this :) – TheEarthkin Sep 29 '17 at 06:40
  • If you read a good book (or tutorial) it will tell you the difference between a *declaration* (telling the compiler something exists somewhere) and a *definition* (saying "this thing exists here"). You *define* the variable in some source file, then *declare* (with the `extern` keyword) the variable in a header file which you include in the source files that needs the variable. I also recommend you learn about the concept of [*translation units*](https://en.wikipedia.org/wiki/Translation_unit_(programming)). – Some programmer dude Sep 29 '17 at 06:43
  • ***However*** with that said, using global variables is usually frowned upon. Perhaps you could pass the variable as an *argument* to the functions that need it? – Some programmer dude Sep 29 '17 at 06:44
  • I will have a look on the wiki page. And i will try to pass it as an argument now. – TheEarthkin Sep 29 '17 at 06:46
  • Also, that header include guard will lead to all instances of the symbol `field` to be replaced with *nothing*. Preprocessor macros (which are defined with `#define`) are recommended to be using upper-case letters only. That distinguish them and make them easier to see, as well as they won't clash with variables that might use all lower-case or mixed case. – Some programmer dude Sep 29 '17 at 06:46
  • I guess there are many things to learn ^^ – TheEarthkin Sep 29 '17 at 06:51
  • Thank you very much. I will now read some pages of information and buy a book before going on with this problem. I need to and want to understand the problem and afterwards try to solve it myself. – TheEarthkin Sep 29 '17 at 06:52

2 Answers2

0

When you declare a variable in your main file, you are not able to use it in another file. (or at least easily)

if you wish to use your field variable in the field.cpp, then you can define it in field.h.

Code for this could be as followed.

#ifndef field

#define field

#include <iostream>
#include <string>
#include <fstream>

void loadLevel(int levelnumber);
void drawField();

char field[20][41];

#endif // !field

Though this will not allow you to use the information you assign to field[i][j] will not be available in your main file.

To do this I would make a function in field.h and field.cpp that returns the value of field[i][j].

  • I did something else. I put this is my field.h `extern char field[20][41];` And this in my field.cpp: `char field[20][41];` – TheEarthkin Sep 29 '17 at 07:26
0

You can use field array in your function by passing it as an argument to your loadlevel function, check this question too if you want to use pointers. So your function will look like:

void loadLevel(int levelnumber,char field[][41]);

OWS
  • 113
  • 11