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 ..
--------------------------------------------------------------------------------------------------------------------------------------------