need to add namespace to header, other file compilations

Recent exercise was to multiple file compile 1) .cpp and another.cpp and 2) .cpp and .h.

Both work as long as I have namespace in the dependent file.

ex:
main.cpp

1
2
3
4
5
6
7
8
9
10
11
#include <iostream>
#include "chapter_1_3.h"

using namespace std;

int main()
{
	int sum = (read_Number()) + (read_Number());
	writeAnswer(sum);
	return 0;
}


works fine, but not without namespacest see below

.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#ifndef CHAPTER_1_3_H_
#define CHAPTER_1_3_H_

using namespace std;

int read_Number()
  {
    int x =0;
   	cout<<"Input value : ";
	cin>>x;
    return x;
   }

int  writeAnswer(int SumNum)
{
	cout<<"Answer :"<<SumNum<<endl;
	return 0;
}


#endif /* CHAPTER_1_3_H_ */ 


It works fine but is it correct? Am I not doing a step that would otherwise delete the namespace need.
Last edited on
A header file only tells you what exists in another file.

main.cpp
1
2
3
4
5
6
7
8
#include "chapter_1_3.h"

int main()
{
	int sum = (read_Number()) + (read_Number());
	writeAnswer(sum);
	return 0;
}

Line 1 there gets replaced with the entire contents of the file "chapter_1_3.h".

chapter_1_3.h
1
2
3
4
5
6
7
#ifndef CHAPTER_1_3_H_
#define CHAPTER_1_3_H_

int read_Number();
int writeAnswer(int SumNum);

#endif /* CHAPTER_1_3_H_ */ 

The actual code must be in a .cpp file:

chapter_1_3.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>

using namespace std;

int read_Number()
  {
    int x =0;
   	cout<<"Input value : ";
	cin>>x;
    return x;
   }

int  writeAnswer(int SumNum)
{
	cout<<"Answer :"<<SumNum<<endl;
	return 0;
}

You must compile and link both main.cpp and chapter_1_3.cpp to produce a working executable.

Read more here:
http://www.cplusplus.com/faq/beginners/multiple-sources/

and here:
http://www.cplusplus.com/faq/compiling/files/

Hope this helps.
You must compile and link both main.cpp and chapter_1_3.cpp to produce a working executable.


What's confusing is that the .cpp/.h works.
Ok- remember now: .cpp for implementation, .h for declaration.

How do you know how to split things up between the two files?

If the current.h/.cpp combination works is it incorrect toleave it so?

Thanks in advance
Last edited on
Declaration is just stuff that says 'something exists, but somewhere else'. Function prototypes are declarations.

Rewriting main.cpp without any chapter_1_3.hpp:
1
2
3
4
5
6
7
8
9
int read_Number();
int writeAnswer(int SumNum);

int main()
{
	int sum = (read_Number()) + (read_Number());
	writeAnswer(sum);
	return 0;
}
<-- These are a prototypes for functions that exists elsewhere (in this case, in "chapter_1_3.cpp")
<-- 'Prototypes' tell the compiler what something looks like: return type, name, arguments, etc.

However, this means that "main.cpp" must know a lot about what is in "chapter_1_3.cpp".

So we tend to keep things as separate as possible: every .cpp has its own .hpp:
- chapter_1_3.hpp tells me what is in chapter_1_3.cpp
- chapter_1_3.cpp has stuff in it

Now the code that uses chapter_1_3's stuff only needs to include "chapter_1_3.hpp" to know all it needs to know about chapter_1_3.cpp. The additional benefit is that if main.cpp has something incorrect in it about chapter_1_3.cpp then the compiler will tell you all about it (in detail), and you can fix main.cpp to match what chapter_1_3 actually has.

Hope this helps.
Thank you for the reply. I worked a lot with multiple file inclusion and got to understand how to add a .cpp file to another.cpp file. Cool. Thanks for pointing me in the right direction.
how to add a .cpp file to another .cpp file

You've not learned anything, then.
Topic archived. No new replies allowed.