Problems with customized header

Hi Guys,
I was trying to learn creating headers. I just created a file Class.h and include iostream in the header instead of the .cpp file. When I tried to compile the .cpp file, I am unable to use cout.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
  // Class.h
#ifndef __IOSTREAM_H_INCLUDED__
#define __IOSTREAM_H_INCLUDE__

class quad
{
	

	private:
		double S[3];

	public:
		double* add(double Parr[3], double Qarr[3])
		{
			for(int i = 0; i < 3; i++)
			{
				S[i] = Parr[i] + Qarr[i];
			}
			return S;
		}

};

#endif



// CLassCall.cpp
#include "Class.h"

using namespace std;

int main()
{
	quad call;

	double P[3], Q[3];
	double* S;

	cout<<"Enter the coefficients of P: \n";
	for(int i = 0; i < 3; i++)
	{
		cin>>P[i];
	}

	cout<<"Enter the coefficients of Q: \n";
	for(int i = 0; i < 3; i++)
	{
		cin>>Q[i];
	}

	cout<<"Sum of P and Q is \n";

	S = call.add(P, Q);

	for(int i = 0; i < 3; i++)
	{
		cout<<S[i]<<endl;
	}

	return 0;
}



error says that cout and cin were not declared in this scope.
closed account (LA48b7Xj)
Change the name of the header guard __IOSTREAM_H_INCLUDED__ to something like MY_CLASS_INCLUDED

and write

#include <iostream>

at the top of the cpp file.
Aren't all the headers included in a header file accesible by the .cpp file by including only the super header?
Aren't all the headers included in a header file accesible by the .cpp file by including only the super header?


Yes. Tell me, what headers are included in your file "class.h"? I see zero headers included in your file "class.h"

I just created a file Class.h and include iostream in the header

No you didn't. Where in your header file have you written #include <iostream> ? Nowhere.



Somewhere, there has to be written
#include <iostream>
if you want to include that header.

It can be in the cpp file, it can be in a header file that is included, it can be in a header file that's included by that header files header file, and so on.

Your code does not have
#include <iostream>
in it, anywhere.

Not in your cpp file. Not in your header file. Your header file includes no other header files. It does not have
#include <iostream>
written in it.
Last edited on
Topic archived. No new replies allowed.