I am making a text based rpg in C++ for a class, I am using a multi file tree structure for the story sections, when 2 of the sections merge to one other story section I am getting an error saying the variable for the section is being redefined, I have heard of using headers but that has not been working because I don't know how to do it! Here is a link to all my files: hey I am over here!
Asked
Active
Viewed 162 times
-1
-
you're not providing any code for us to help you with. – Ven Apr 21 '16 at 20:09
-
@Ven Check out the link it has my code, it is multiple files and that would take forever and if I make changes I would have to rewrite. – Ph9214 Apr 22 '16 at 14:57
-
don't give us a link to a google drive. Reduce your problem to something you can show us. – Ven Apr 22 '16 at 14:58
1 Answers
1
You might be forgetting to add include guards to the tops of your files. These keep your files from being compiled more than once. You could have also just defined the variable twice in the same scope or function.
Example (from Wikipedia)
#ifndef GRANDFATHER_H
#define GRANDFATHER_H
// this code is only compiled once thanks to the
// preprocessor directive #ifndef
struct foo {
int member;
};
#endif /* GRANDFATHER_H */
Miles Smith
- 94
- 3
-
1Any C++ compiler worth it's salt will be able to handle `#pragma once` instead of the explicit definitions. – Colin Basnett Apr 21 '16 at 20:01
-
`#pragma once` is **nonstandard**. See this SO answer: http://stackoverflow.com/questions/23696115/is-pragma-once-part-of-the-c11-standard – Miles Smith Apr 21 '16 at 20:05
-
1Good point, but I never said it was a standard; unless the OP is hellbent on compiling on Solaris (https://en.wikipedia.org/wiki/Pragma_once#Portability), there should not be any portability issues using `#pragma once`. – Colin Basnett Apr 21 '16 at 20:10