HEADER FILE PROBLEM !!

I am using gcc , I wrote the following header file-
name test_header.h

1
2
3
4
5
6
7
8
9
10
11
12
13
#ifndef _test_header_h_
#define _test_header_h_
#include<iostream>
using namespace std;
int add(int x, int y)
{
int c= x+y;
return c;
};
#endif


 


I successfully compiled it and added it into the include directory ..

But when I am writing the following c++ code (named test1.cpp)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include<test_header.h>
using namespace std;
int main()
{
int a,b;
cout << "\nEnter a = ";
cin >> a;

cout << "\nEnter b =";
cin >> b;

int c=add(10,20);

cout << "\n\n Add= " << c << "\n\n";
cout << "\n\nTEST SUCCESSFUL \n\n";
return 0;
}



I am getting following errors->

error: ‘cout’ was not declared in this scope
error: ‘cin’ was not declared in this scope.

AND PLEASE TELL ME HOW TO FETCH THE DEFAULT IOSTREAM NAMESPACE THROUGH MY OWN HEADER FILE.


====================================================================================
===================================================================================


@ cppprogrammer297
Thanx
When I removed ; from line number 9 , It started working fine (i.e-- It successfully fetched the namespace also from iostream & also Working fine)..

1
2
3
4
5
6
7
8
9
10
11
#ifndef _test_header_h_
#define _test_header_h_
#include<iostream>
using namespace std;
int add(int x, int y)
{
int c= x+y;
return c;
}
#endif 


BUT...

Many of my friends said that It is not a good practice to fetch other namespaces (Even default namespace ie "std" ) from my own header file .. Header file is best suited for fetching functions ..
Is this right ??
@giblit
@MikeyBoy
@L B
and all other readers please describe it & comment on it ..
--------------------------------------------------------------------------------------------------------------------------------------------
Last edited on
closed account (jwkNwA7f)
There shouldn't be a semicolon at the of the add function:
1
2
3
4
5
6
7
8
9
10
#ifndef _test_header_h_
#define _test_header_h_
#include<iostream>
using namespace std;
int add(int x, int y)
{
int c= x+y;
return c;
} // <--Edit: here
#endif 
Last edited on
anyways it should be " " and not < > around your header when you include it and it should be in the same directory as your source file/project.
The reason you are getting those errors are probably because the header isn't be accessed so therefore neither is the namespace which is bad practice.
I would recommend std::.
As far as the semicolon that shouldn't affect it. You can put 300 semicolons and it would make no difference other than make the code look hideous.

Just a head up using a header like that is pointless imo. I would only really use one for a class/struct/namespace
@subhransu:

Have you tried including <iostream> directly in test.cpp?

@giblit:

The reason you are getting those errors are probably because the header isn't be accessed so therefore neither is the namespace which is bad practice.


What do you mean by "isn't be [sic] accessed"? If the compiler can't find the header file, then it would display an error message explicitly stating that it can't find it, and presumably the OP would have told us that.

Let me ask you this then MikeyBoy.
If the header file with this inside of it
1
2
#include <iostream>
using namespace std;
Why the heck would he need to put #include <iostream> inside of the main source file that is including the header file and all the things inside of it.

Try and compile this
1
2
3
4
5
6
7
8
9
10
11
12
//test.h
#include <iostream>
using namespace std;

//main.cpp
#include "test.h"

int main()
{
    cout << "Hello World!" << endl;
    return( 0 );
}
Hello World!

Process returned 0 (0x0)   execution time : 0.230 s
Press any key to continue.


Pointless header file for something like this but it's just an example.
This looks like what you are trying to do subhransu
http://www.learncpp.com/cpp-tutorial/19-header-files/
Let me ask you this then MikeyBoy.
If the header file with this inside of it

1
2
#include <iostream>
using namespace std;


Why the heck would he need to put #include <iostream> inside of the main source file that is including the header file and all the things inside of it.


You shouldn't. Obviously something strange is going on. The question is - what?

My guess is that the wrong header file is somehow being included. My suggestion was a step towards diagnosing what's wrong.

And you didn't answer my question.

Last edited on
giblit wrote:
Let me ask you this then MikeyBoy.
If the header file with this inside of it
1
2
#include <iostream>
using namespace std;
Why the heck would he need to put #include <iostream> inside of the main source file that is including the header file and all the things inside of it.
1. Never place "using namespace x;" or "using x::y;" in a header file.
2. Never rely on another header to include other headers for you. Even if you happen to know that HeaderA.hpp includes HeaderB.hpp, you still should include HeaderB.hpp in case HeaderA.hpp eventually stops needing to include HeaderB.hpp for itself.
@ cppprogrammer297
Thanx
When I removed ; from line number 9 , It started working fine (i.e-- It successfully fetched the namespace also from iostream..

1
2
3
4
5
6
7
8
9
10
11
#ifndef _test_header_h_
#define _test_header_h_
#include<iostream>
using namespace std;
int add(int x, int y)
{
int c= x+y;
return c;
}
#endif 


BUT...

Many of my friends said that It is not a good practice to fetch other namespaces (Even default namespace ie "std" ) from my own header file .. Header file is best suited for fetching functions ..
Is this right ??
@giblit
@MikeyBoy
@L B
and all other readers please describe it & comment on it ..
I wouldn't even bother using namespace std;
L B wrote:
1. Never place "using namespace x;" or "using x::y;" in a header file.
2. Never rely on another header to include other headers for you. Even if you happen to know that HeaderA.hpp includes HeaderB.hpp, you still should include HeaderB.hpp in case HeaderA.hpp eventually stops needing to include HeaderB.hpp for itself.


EDIT::MISSING a line.
Last edited on
Many of my friends said that It is not a good practice to fetch other namespaces (Even default namespace ie "std" ) from my own header file .. Header file is best suited for fetching functions ..
Is this right ??

Yes, this is correct. Putting "using namespace" in a header file is bad practice, because it forces every single file that includes the header file to pull everything in that namespace into the global namespace.

In your case, you don't even use anything from std in your header file, so I don't even understand why you thought you should it put it there in the first place.
Topic archived. No new replies allowed.