0

For example I have 2 cpp files: f1.cpp and f2.cpp, and also a header file: xxx.h.

f1.cpp has the following source code:

#include <iostream>
#include "xxx.h"

using namespace std;

int main ()
{
    rect rplace;
    polar pplace;
    cout<<"Enter the x and y values: ";
    while (cin>>rplace.x>>rplace.y)
    {
        pplace=rect_to_polar(rplace);
        show_polar(pplace);
        cout<<"Next two numbers (q to quit): ";
    }
    cout<<"Done.\n";
    return 0;
}

f2.cpp source code:

#include <iostream>
#include <cmath>
#include "xxx.h"

polar rect_to_polar (rect xypos)
{
    using namespace std;
    polar answer;
    answer.distance=sqrt(xypos.x*xypos.x+xypos.y*xypos.y);
    answer.angle=atan2(xypos.y, xypos.x);
    return answer;
} 

void show_polar (polar dapos)
{
    using namespace std;
    const double Rad_to_deg=57.29577951;
    cout<<"distance="<<dapos.distance;
    cout<<", angle= "<<dapos.angle*Rad_to_deg;
    cout<<"degrees\n";
}

And xxx.h:

struct polar
{
    double distance;
    double angle;
};

struct rect
{
    double x;
    double y;
};

polar rect_to_polar (rect xypos);
void show_polar(polar dapos);

I thought that there should be a compiler error because the headers xxx.h and iostream are included two times: once in f1.cpp and once in f2.cpp. But everything was compiled, so I don't understand how it can work.

gx_
  • 4,690
  • 24
  • 31
Sunrise
  • 1,311
  • 3
  • 18
  • 28
  • Side note: This is not related to the question (and actually doesn't change anything in this precise example), but of course xxx.h should be protected by “include guards” (or a `#pragma once`). – gx_ Nov 16 '13 at 21:02
  • @gx_ By "include guards", do you mean `#ifndef` `#endif`? – Sunrise Nov 16 '13 at 21:06
  • @Sunrise [Yes](http://en.wikipedia.org/wiki/Include_guard) – gx_ Nov 16 '13 at 21:09

1 Answers1

4

The preprocessor simply reads the header files and puts them into the translation units where the #include directive is. If one header file is included in one source file, only that file knows about the declarations in that header file.

You also should know the difference between declarations and definitions. When you declare something, you just tells the compiler that "this thing exists and is of type this-and-that". When you define something, you tell the compiler "this is the thing I declared before".

You can have multiple declarations of the same thing. because they only act as meta-data for the compiler, and are not used outside its translation unit. You can however only have one definition of something. That's why you can't define global variables/functions in header files included in multiple source files.

Also, In this answer I talk about "source files", "header files" and "translation units". Header files are the files you #include. Source files is the files that does the including (so to speak). And a translation unit is the complete preprocessed source, complete with the source and all included header files, and which is passed on to the actual compiler.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • If header file contain only declarations and I can have multiple declaration of the same, then why I cannot in header file write twice polar struct declaration? However I can double function declaration. – Sunrise Nov 16 '13 at 20:34
  • @Sunrise Some things, like functions, can be declared multiple times in the same translation unit *if they are declared the same*. But things like structures and classes are a little different, because actually *define* the structures, but a structure definition is like a declaration, it doesn't in itself generate any code. – Some programmer dude Nov 16 '13 at 20:48
  • If I'm properly understood you, structure declaration in reality is structure definition that behaves like declaration. And because it's behaves like declaration I can use it in header files, but because it's also definition that's why I can't write it twice in header file. Then the question appears why structure declaration is actually structure definition? – Sunrise Nov 16 '13 at 21:01
  • 3
    @Sunrise A “structure declaration”, often called a “_forward-declaration_”, is just “`struct S;`”. Like a function declaration, it can appear multiple times in the same TU (translation unit). But a structure _definition_ (like “`struct S { int x; void f(); };`”) defines the names and types of its members (needed to create variables and use them), so it must be unique in a given TU. But more than one TU may need the definition, so C++ allows duplicate definitions of the same struct in a program, but maximum one per TU, and all must be identical (and header inclusion permits that). – gx_ Nov 16 '13 at 21:32
  • @gx_ Thanks for such detailed answer, now everything is clear for me. – Sunrise Nov 16 '13 at 21:52