-1

I'm getting this error everytime i try to define a class on header's project file is there is any tips to avoid these errors ?

everytime i run this code the debug respond with this message :unknown type cout

here is the C++ (header.h file code) :

//
//  start.h
//  start_practice
//
//  Created by Macbook Pro on 11/22/15.
//  Copyright (c) 2015 Macbook Pro. All rights reserved.
//

#ifndef __start_practice__start__
#define __start_practice__start__

#include <iostream>
using namespace std ;


cout << "hello" ; <--- everytime i run this code the debug respond with this message :unknown type cout 

#endif /* defined(__start_practice__start__) */
Steephen
  • 14,645
  • 7
  • 40
  • 47
Amir Bennasr
  • 216
  • 1
  • 2
  • 10

1 Answers1

1

You need to put actions like cout << "hello" into a function.

Things that are not declarations cannot float about in empty space. How would the computer know when you execute them?

So, in fact, the compiler assumes you must be trying to write a variable, type or function declaration. Because you're not, the resulting error message is admittedly a little confusing.

When you make your function, it does not matter whether you define it in a header or a source file (.cpp) though, for reasons I shan't go into here, you generally want to do so in the latter.

In short, everything should generally be in a source file, except for function template definitions and inline functions that you really want to define in a header for some reason. Other functions will work okay too as long as you only include your header into a single translation unit.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055