Can not figure out the .h file concept at all

I have three files. When i try to compile I get sooo many errors I have no clue what to do.

main.cpp
1
2
3
4
5
6
  #include <iostream>
  #include <iomanip>
  using namespace std;
  #include "Clock.h"

    code


Clock.h
1
2
3
4
5
6
7
8
9
#ifndef Clock_H
#define Clock_H 1
#include <iostream>
class Clock {
   private: yada yada
   public: yada yada
   friend ostream &operator<<(ostream &, Clock&);
};
#endif 


Clock.cpp
1
2
3
4
5
#include <iostream>
#include "Clock.h"
   ostream &ostream &operator<<(ostream &out, Clock &time){
     code }
   Clock::ya das}



When I try to get it compiled these errors all occur.
in main.cpp:
Clock.h:35:9 error: 'ostream' does not name a type
'cout' was not declared in this scope
'endl' was not declared in this scope
'cout' was not declared in this scope
'endl' was not declared in this scope
'cin' was not declared in this scope
in Clock.cpp:
Clock.h:35:9: error: 'ostream' does not name a type
'ostream' does not name a type
'cout' was not declared in this scope
suggested alternative: /usr/include/c++/4.6/iostream::62:18: note: 'std::cout'

'endl' was not declared in this scope
suggested alternative: /usr/include/c++/4.6/ostream:543:5: note: 'std::endl'


I have no clue what I need to change...
Last edited on
Clock.h

1
2
3
4
5
class Clock{
public:
	friend ostream &operator<<(ostream&, const Clock&);
	...
};


Clock.cpp

1
2
3
4
#include "Clock.h"
ostream &operator<<(ostream& out, const Clock& time){
	...
}
Last edited on
What was changed?
Well... our << operator doesn't need to change our Clock object call me paranoid but if I pass something by reference that I don't want changed I tag on const.

"Reference because it is fast and const to make it safe"

and

You posted:

ostream &ostream &operator<<(ostream &out, Clock &time)

I posted:

ostream &operator<<(ostream& out, const Clock& time)
I made those consts if thats all that was changed.
It still has errors but a lot less.
It still says 'ostream doesn't name a type and
'cout' was not declared in this scope
'endl' was not declared
with suggested alternatives for both
Oh the two ostreams was a typo that wasn't in the actual code sorry
Okay I fixed something else I saw but it still says in clock.h and clock.cpp that 'ostream' does not name a type
I noticed using namespace std; is in your main and not in your header file. And there are reasons not to use that so try.

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

class Clock {

public: 
		friend std::ostream &operator<<(std::ostream &, const Clock&);
};

std::ostream &operator<<(std::ostream& out, const Clock& time){

}


And either use namespace std; or std::cout, std::cin, std::endl;, and so on as you need.
Last edited on
I have line 9 in the Clock.cpp just to make that clear. Hopefully thats right.

When i put std:: in front of both of these it gives a lot more errors like
declaration of 'operator<<' as non-function
expected ';' at end of member declaration
expected ')' before '&' token
declaration of 'operator<<' as non-function
'ostream' was not declared in this scope
suggested alternative 'std::ostream'
'out' was not declared in this scope
expected primary-expression before 'const'

...so confused..
Okay wait. I put std:: in front of all the ostreams then put std:: in front of the setw and endl and setfill. All that it has problems with now is

assignment of member 'Clock::mHours' in read-only object

at this part in clock.cpp I have:
 
{ time.mHours = time.mHours - 12;

mHours is a member variable but I can use it in the rest of the code so why not here?
Topic archived. No new replies allowed.