-3

I have a project where there is a class item in a header item.h. I need to use the item class in both my main.cpp file and my player.h header.

There are example files to explain my situation :

main.cpp:

#include "player.h"
#include "item.h"
#include <vector>

std::vector<Item> items;

int main(){
    Item i;
    Player p;
    p.move(10);
    i.ID = 10;
    return 0;
};

player.h:

#include "item.h"
#include <vector>

class Player{
    public:
        std::vector<Item> inventory;
        int x;
        void move(int i){x += i};
};

item.h:

#include <string>

class Item{
    public:
        int ID;
        std::string name;
};

It may contain errors as I didn't compiled it, and just wrote it for you to understand my situation. In my real project, I get errors for redefinition of the Item class. I'm new to C++.

Babooch
  • 5
  • 3
  • 1
    Look up "include guards" for how to avoid problems when a header is included more than once. – Mark Ransom Jul 18 '22 at 17:06
  • 1
    Take a look at [#pragma once](https://learn.microsoft.com/en-us/cpp/preprocessor/once?view=msvc-170) – thebear8 Jul 18 '22 at 17:09
  • @thebear8 -- with the caution that `#pragma once` is not standard C++ and cannot be implemented correctly on some build platforms. Which is why the usual include guards aren't going to go away. – Pete Becker Jul 18 '22 at 17:11
  • @PeteBecker Absolutely. Include guards are the portable way of doing it, however, [just about any compiler nowadays supports it](https://en.wikipedia.org/wiki/Pragma_once#Portability) and it seems very unlikely the op is working on platform that does not support it. – thebear8 Jul 18 '22 at 17:19
  • How this question is related to C? Don't spam tags – qrdl Jul 18 '22 at 18:39

2 Answers2

2

The solution you seek is Include Guards.

'Include guards' prevent the inclusion of a header file more than once. If a file is included multiple times you will get an error because you are defining the content of the file multiple times.

In your case using include guards in item.h will fix your error, but it is recommended to use it in all .h files.

An example of how the compilers process you includes:

You got an header file foo.h:

#ifndef FOO_H
#define FOO_H

void foo(){
}

#endif // FOO_H

In your main file (main.c), you include file.h:

#include "foo.h"

void main() {
    foo();
}

After the linking process the compiler would generate the following file:

void foo(){
}

void main() {
    foo();
}

The compiler took all of the .h and .c files and stitched them together into one file.

If there was another header file (e.g. bar.h) that includes foo.h and main.c also includes bar.h, main will include foo.h directly but also through bar.h, so foo function would be declared twice!

Luckily in this example foo.h has header guards which practically means foo.h cannot be included more than once.

I recommend reading about the preprocessor and specifically about define. See https://learn.microsoft.com/en-us/cpp/preprocessor/hash-define-directive-c-cpp?view=msvc-170

shahar
  • 355
  • 2
  • 18
0

You need to do this in item.h:

#ifndef SOME_WORD
#define SOME_WORD // the same as above
//item.h content
#endif

You can now import item.h in all the files that you want.

Babooch
  • 5
  • 3