0

I'm new to c++ and I'm on the "hello world" program and I keep getting the error

"cout" does not name a type I'm using geany on Ubuntu if that makes a difference and here is my code:

#include <iostream>

int main ()
{
extern cout << "hello world!";
    return 0;
}

I don't want to make a new question so I'm going to add it here

With the revisions supplied it will compile now, but when i run the program i get the error

./geany_run_script.sh: 5: ./geany_run_script.sh: ./hello: not found

any ideas on that?

Chance Patrick
  • 7
  • 1
  • 1
  • 5

2 Answers2

3

Change extern to std::. The first problem is that extern is only valid before the name of a type, so that's what the compiler is complaining about. The second is that cout is defined in the namespace std, so you need to tell the compiler to look there. The good thing is that the code doesn't say using namespace std;.

Pete Becker
  • 74,985
  • 8
  • 76
  • 165
1

Change:

extern cout << "hello world!";

too

std::cout << "hello world!";  // You probably want \n on the end.

This is because cout is an object defined in the namespace std. Thus you need let the compiler know where to find it by prefixing it with std::. There are a couple of alternative techniques but this is the best in my opinion.

Alternative one: Use the using directive

using std::cout;
cout << "hello world!";

The using std::cout; tells the compiler that there is an object in std called cout that we want to use locally and it is brought into the current context thus allowing you to use it directly.

Martin York
  • 257,169
  • 86
  • 333
  • 562