repeating intructions in .h and .cpp files

P

p_adib

Well, I tried this test:

////// include_iostream.h START //////////
#include<iostream>
////// include_iostream.h END ////////////


////// including_twice.cpp START //////////

#include<iostream>

#include "include_iostream.h"

using namespace std;

int main()
{
cout << "Mainly worked" << endl;
return 0;
}

////// including_twice.cpp END //////////

And it worked with no errors or warnings. When is it the case that
lines that are in .h file cause conflicts with code lines in the .cpp
file that includes it?
 
B

benben

Well, I tried this test:

////// include_iostream.h START //////////
#include<iostream>
////// include_iostream.h END ////////////


////// including_twice.cpp START //////////

#include<iostream>

#include "include_iostream.h"

using namespace std;

int main()
{
cout << "Mainly worked" << endl;
return 0;
}

////// including_twice.cpp END //////////

And it worked with no errors or warnings. When is it the case that
lines that are in .h file cause conflicts with code lines in the .cpp
file that includes it?

It's because the standard header <iostream> has its own include guard.
Your second inclusion of the standard header simply has no effect.

To illustrate a conflict, try:

// header1.hpp////////////////
int a = 0;

// header2.hpp////////////////
#include "header1.hpp"

// main.cpp///////////////////
#include "header1.hpp"
#include "header2.hpp"

int main(){}

Regards,
Ben
 
V

Victor Bazarov

Well, I tried this test:

////// include_iostream.h START //////////
#include<iostream>
////// include_iostream.h END ////////////


////// including_twice.cpp START //////////

#include<iostream>

#include "include_iostream.h"

using namespace std;

int main()
{
cout << "Mainly worked" << endl;
return 0;
}

////// including_twice.cpp END //////////

And it worked with no errors or warnings. When is it the case that
lines that are in .h file cause conflicts with code lines in the .cpp
file that includes it?

If all is done right, never. Read about "double inclusion guards".

V
 
P

p_adib

To illustrate a conflict, try:
// header1.hpp////////////////
int a = 0;

// header2.hpp////////////////
#include "header1.hpp"

// main.cpp///////////////////
#include "header1.hpp"
#include "header2.hpp"

int main(){}

Thanks Ben.
So I did it, and I think the error code follows what you're saying:

g:\my documents\f-2006\446\a3\test\include & define
test\ggtestdoubleinclusion\definetest\header1.h(2) : error C2374: 'a' :
redefinition; multiple initialization
g:\my documents\f-2006\446\a3\test\include & define

test\ggtestdoubleinclusion\definetest\header1.h(2) : see declaration of
'a'
g:\my documents\f-2006\446\a3\test\include & define

test\ggtestdoubleinclusion\definetest\main.cpp(5) : warning C4508:
'main' : function should return a value; 'void' return type assumed
Error executing cl.exe.

main.obj - 1 error(s), 1 warning(s)
 
P

Pete Becker

And it worked with no errors or warnings. When is it the case that
lines that are in .h file cause conflicts with code lines in the .cpp
file that includes it?

Most compilers have an option to only run the preprocessor. When you're
trying to figure out how the preprocessor works, you can preprocess the
code and look at the result. That can be pretty longwinded, so it's most
useful for simple experiments. Write a one- or two-line header, #include
it in a simple source file, and run it through the preprocessor. Then
#include it a second time and try it again.

--

-- Pete
Roundhouse Consulting, Ltd. -- www.versatilecoding.com
Author of "The Standard C++ Library Extensions: a Tutorial and
Reference." For more information about this book, see
www.petebecker.com/tr1book.
 
P

p_adib

Pete said:
Write a one- or two-line header, #include
it in a simple source file, and run it through the preprocessor. Then
#include it a second time and try it again.

Thanks Pete.
I would like to try it out, but I am confused as to how to run a
preprocessor. Do c++ dev. environments generally allow this option?
 
V

Victor Bazarov

Thanks Pete.
I would like to try it out, but I am confused as to how to run a
preprocessor. Do c++ dev. environments generally allow this option?

Yes. You need to look in your compiler documentation for "generate
preprocessor output" option. Example, g++ and VC++ have -E to output
the result of preprocessing to standard out.

V
 
B

BobR

Victor Bazarov wrote in message ...
Yes. You need to look in your compiler documentation for "generate
preprocessor output" option. Example, g++ and VC++ have -E to output
the result of preprocessing to standard out.
V

OP:

For g++, there is also the:

-save-temps
Store the usual "temporary" intermediate files permanently; place them
in the current directory and name them based on the source file. Thus,
compiling foo.c with -c -save-temps would produce files foo.i and foo.s,
as well as foo.o. This creates a preprocessed foo.i output file even
though the compiler now normally uses an integrated preprocessor.


watch out, files are big (mostly full of 'air'). Scroll to the bottom of a
myprog.ii (or *.i if C) and work your way back up.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,954
Messages
2,570,116
Members
46,704
Latest member
BernadineF

Latest Threads

Top