473,394 Members | 1,841 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

repeating intructions in .h and .cpp files

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?

Nov 10 '06 #1
7 1469
p_****@encs.concordia.ca wrote:
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 <iostreamhas 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
Nov 11 '06 #2
p_****@encs.concordia.ca wrote:
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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Nov 11 '06 #3
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)

Nov 11 '06 #4
p_****@encs.concordia.ca wrote:
>
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.
Nov 11 '06 #5
Pete Becker wrote:
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?

Nov 22 '06 #6
p_****@encs.concordia.ca wrote:
Pete Becker wrote:
>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?
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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Nov 22 '06 #7

Victor Bazarov wrote in message ...
>p_****@encs.concordia.ca wrote:
>Pete Becker wrote:
>>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?

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.

--
Bob R
POVrookie
Nov 23 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: Wescotte | last post by:
Here is a small sample program I wrote in PHP (running off Apache 1.3.31 w/ PHP 5.0.1) to help illustrates problem I'm having. The data base is using DB2 V5R3M0. The client is WinXP machine using...
4
by: James Collier | last post by:
Is it possible to capture the results of repeating group matches in the python regular expression module? To illustrate, what I want is: >>> re1 = re.compile("(W)(X)+(Y)"); >>> mo1 =...
4
by: Dean | last post by:
I am a developer who works with MS SQL Server a lot, but I'm far from an expert. I am revamping an appointment scheduling system that allows for appointments to reoccur daily, weekly, monthly and...
1
by: Wolfgang Lipp | last post by:
my question is: do we need container elements for repeating elements in data-centric xml documents? or is it for some reason very advisable to introduce containers in xml documents even where not...
0
by: dino07 | last post by:
Hi All, I am currently trying to do the following: 1. insert a value into a repeating table from a drop-down list( secondary storage) when the user click the "Add" button positioned next to the...
11
by: Christoph Boget | last post by:
When building a form using Infopath, you can define a repeating section and stick form fields in that section. I'm curious if ASP.NET has a similar control to make it easy to design something...
2
by: nickheppleston | last post by:
I'm trying to iterate through repeating elements to extract data using libxml2 but I'm having zero luck - any help would be appreciated. My XML source is similar to the following - I'm trying to...
3
by: bagelman | last post by:
Hi, I want to find repeating words in a long string with Regular Expressions. I tried to write a regular expression but it didn't work. "\b(?<word>\w+)\s+(\k<word>)\b" This RegEx finds...
3
by: =?Utf-8?B?U2hhbG9t?= | last post by:
I have installed iis 7 on my win vista home premium Sony Vaio laptop, and then tried executing downloaded sample working asp scripts in the wwwroot directory to try out. The first asp script...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.