1

I'm trying to learn c++ but a simple method like "cout" and "cin" does not exist this is my code:

#include "stdafx.h"
#include <iostream>


int _tmain(int argc, _TCHAR* argv[])
{
    cout>>"hello";
    return 0;
}

there is an error that says "error C2065: 'cout' : undeclared identifier"

and "IntelliSense: identifier "cout" is undefined"

user2302416
  • 87
  • 2
  • 11
  • 6
    Your angle brackets are backwards. Try `std::cout << "hello"`. – Bill Carey Jul 29 '13 at 18:06
  • 3
    `cout` is in the `std` namespace. Try `std::cout << "hello";` – Praetorian Jul 29 '13 at 18:06
  • 3
    You're missing `std::` and you mixed up `>>` and `<<`. – Rapptz Jul 29 '13 at 18:06
  • I just open a project in visual studio 2012. I only added the: cout<<"hello"; – user2302416 Jul 29 '13 at 18:09
  • 2
    @user2302416: Yes, and that's why it's not working. Change `cout<<"hello";` to `std::cout << "hello";` -- or, better yet, `std::cout << "hello\n";`. You *could* drop the `std::` by adding `using namespace std;`; you'll see a lot of code that does that, but it's often considered to be a bad habit. – Keith Thompson Jul 29 '13 at 18:41

4 Answers4

8

cout is declared in the std namespace:

int _tmain(int argc, _TCHAR* argv[])
{
    std::cout << "hello";
    return 0;
}

You're also doing it wrong. Note how I have the angle brackets aboove.

John Dibling
  • 99,718
  • 31
  • 186
  • 324
1

Add #include <iostream> to stdafx.h. I was having trouble with that and it worked for me.

Dan Getz
  • 8,774
  • 6
  • 30
  • 64
0

Adding using namespace std; may help, and also do cout << "hello" not >>.

Iosif Murariu
  • 2,019
  • 1
  • 21
  • 27
  • what's bad at using a namespace? – Iosif Murariu Jul 29 '13 at 18:09
  • 1
    `using namespace std` can create many more problems than it solves, especially when done in a header file. – John Dibling Jul 29 '13 at 18:09
  • @IosifM. It's considered bad for [many, many reasons](http://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice) – Praetorian Jul 29 '13 at 18:10
  • @Praetorian, I agree that "collisions" may occur on "custom-made" namespaces, but still... namespace std? – Iosif Murariu Jul 29 '13 at 18:11
  • It wasn't, but you did not explain how doing `using namespace std` can be a problem, even in a source file, so to a newbie this could be taken as *the* solution. Hence you are effectively suggesting that this is an appropriate solution in all contexts, which it's not. – John Dibling Jul 29 '13 at 18:12
  • Don't teach bad habits regardless of it. Namespace pollution is bad. See [here](http://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice). – Rapptz Jul 29 '13 at 18:12
  • pulling in a full namespace for something this simple is plainly overkill anyways. – user2366842 Jul 29 '13 at 18:12
  • It's even worse with `namespace std` because it contains all sorts of generic sounding names, such as `find` or `accumulate` (or practically anything from ``) – Praetorian Jul 29 '13 at 18:13
  • OK, I guess you've proven your point pretty well :). Was just curious why it's good to avoid namespaces. – Iosif Murariu Jul 29 '13 at 18:14
  • so I should'nt use the namespace? – user2302416 Jul 29 '13 at 18:17
  • @user2302416: No, you should not use a `using namespace` declaration until you know exactly what you're doing. – John Dibling Jul 29 '13 at 18:18
  • for std specifically you're pretty much always better off calling the namespace directly as needed because of the sheer amount of different functions that it implements....otherwise there's always the old C way of using printf instead of cout. – user2366842 Jul 29 '13 at 18:21
0

cout is in std so you to have use using namespace std. And for cout operator is like <<. >> this is for input i.e cin.

#include "stdafx.h";
#include "iostream";
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
    cout<<"hello";
    system("pause");
    return 0;
}
Mehtab
  • 453
  • 5
  • 17