tags:

views:

158

answers:

6
+3  Q: 

redefinition c++

hi, how does header including in c++ work? I have the classes already implemented in .h file and when there is #include in two files, there's this error:

files.h:14:7: error: redefinition of ‘class abstract_file’
files.h:14:20: error: previous definition of ‘class abstract_file’`

multiple times for each class and enum. Can anyone explain this?

+2  A: 

You can only include a definition one time but headers can be included multiple times. To fix that, add:

#pragma once

to the top of each header file.

While #pragma once is relatively common, if you are using an older compiler it may not be supported. In that case, you need to fall back on manual include guards:

#ifndef MY_HEADER_H
#define MY_HEADER_H

...

#endif

(note that you need to replace MY_HEADER_H with a unique string for each header file)

R Samuel Klatchko
Note that this only works with the MSVC C++ compiler.
Eddy Pronk
Note that this also works with GCC
abababa22
@EddyPronk - that is not correct. gcc has also supported this at least since version 3.4.5 (and maybe even older, but 3.4.5 is the oldest version I have available for testing).
R Samuel Klatchko
@Eddy: It works with `gcc` as well. At one time the `gcc` documentation said it was deprecated, but that has been removed and it is fully supported.
Nate
@Nate, @Samuel: It might work with GCC and MSVC but that doesn't mean it's standard C++, and it's certainly not portable C++. Best to teach people to write portable code than to use handy shortcuts. Will it work in other compilers? Possibly. Personally I wouldn't want to waste my time going through a hundred headers fixing them to be standard on such a trivial thing before I can use a different compiler.
Matt Curtis
+6  A: 

Using include in C++ simply takes the file included and splats the contents into where it is included. To do this without worrying about multiple includes of the same file, you want to use header guards. Use this basic format for all header files:

#ifndef FILENAME_H
#define FILENAME_H

class foo (or whatever else is in the file!) {
    ...
};

#endif
Justin Ardini
A: 

What you can do is to guard your header from multiple inclusions:

#ifndef MY_HEADER__
#define MY_HEADER__

/* your stuff goes here! */

#endif

You can also use:

#pragma once

but it is not standard although it is supported by many compilers.

AraK
+2  A: 

header files usually define a unique symbol so that they are only included once.

e.g.

#ifndef _myheader_h
#define _myheader_h

// rest of header goes here

#endif

Some compilers support

#pragma once

See Pragma Once on wikipedia.

mdma
A: 

Use include guards in header files:

#ifndef FILES_H
#define FILES_H

struct foo {
    int member;
};

#endif // FILES_H

This makes sure that your header will only get included once.

Another method is to use #pragma once in your header files, but it's not standard C. It's supported by Visual C, GCC and Clang though, so most likely it's ok to use.

abababa22
+2  A: 

While many people have solved your error for you, it seems that nobody has answered your initial question:

how does header including in c++ work?

When the preprocessor finds an #include directive it replaces it by the entire content of the specified file.

You can read more on preprocessor directives at cplusplus.com.

Update: To illustrate this, you could try the following if you have gcc handy:

echo '#include <iostream>' > test.cxx 
gcc -E test.cxx

You'll see the contrents of iostream whizz past your eyes as the preprocessed source code is sent to standard output.

Johnsyweb