6

So, I have this question. Why does cout throws

error C2065: 'cout' : undeclared identifier

I am using Visual Studio 2012 as an IDE and I am writing a school project. I have everything done except an example file. So I am trying to write something on the screen like this:

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

int main()
{
    cout<<"example";

    return 0;
}

So the problem is with cout... printf works fine, but I want to use cout.

EDIT: I've changed "" to <> but it is not helping. Also I am using this code only for example... This is not the whole project.

Dekay
  • 111
  • 1
  • 2
  • 12
  • Try `#include ` – Andy Prowl May 31 '13 at 13:42
  • And use `std::cout` or use the namespace. – Dirk May 31 '13 at 13:42
  • 4
    I believe the precompiled header must be the first line in the source. – chris May 31 '13 at 13:49
  • @chris Yes, assuming the project actually uses precompiled headers. – Angew is no longer proud of SO May 31 '13 at 13:49
  • 2
    @Angew, Well, judging by the stdafx.h in the source, I figured it did. – chris May 31 '13 at 13:50
  • As Dirk pointed out, do not use `using namespace std;`. It will interfere when using using other namespace(s). – TheBlueCat May 31 '13 at 16:55
  • The quotes vs. angle brackets on the `#include` directive, despite generating a great deal of traffic, has **nothing** to do with the problem here. Yes, it's better style to use angle brackets for system headers, but style guidelines do not determine validity of code. I'm tempted to change the original question to eliminate this red herring, but that would make the answers look too much like nonsense. The problem is Microsoft's notion of precompiled headers: they don't follow the language rules. – Pete Becker May 31 '13 at 17:50

6 Answers6

17

stdafx.h shall be the first include directive in your source file.

Switch files and convert the second include to <>, as other suggested.

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

See this post for more information.

Community
  • 1
  • 1
Matthieu Rouget
  • 3,289
  • 18
  • 23
  • 1
    thx, I just found that out by myself, but you are the only one with the actual fix :D :D – Dekay May 31 '13 at 13:57
  • @user2440586, To be fair, it *is* a lot better to use angle brackets when including standard headers. – chris May 31 '13 at 14:04
12

First of all:

#include <iostream>

instead of #include "iostream"

Secondly, it is generally considered bad practice to write using namespace std;, even though most courses start with that. It is better to only use what you actually need, in your case:

using std::cout;

Marc Claesen
  • 16,778
  • 6
  • 27
  • 62
5
 #include "iostream"

should be

 #include <iostream>

Quoting from this post:difference-between-iostream-and-iostream-quotes-in-include

By courtesy of @Jerry Coffin's answer:

When you use < >, the compiler only looks in the system-designated directory/directories (e.g., whatever you've set in the include environment variable) for the header.

When you use " ", the compiler looks in the local directory first, and if that fails, re-searches just like you'd used < >. Technically, (i.e., according to the standard) that doesn't have to be the "local" directory, but that's how it works in essentially every compiler of which I'm aware).

EDIT:

However, the root cause is that stdafx.h is a precompiled header. Visual C++ will not compile anything before the #include "stdafx.h" in the source file, unless the compile option /Yu'stdafx.h' is unchecked (by default); it assumes all code in the source up to and including that line is already compiled. However, it is still better to use <> with iostream not to confuse reader of the code.

Community
  • 1
  • 1
taocp
  • 23,276
  • 10
  • 49
  • 62
  • 1
    The problem I see is that you contradict your answer with the quotation. My money's on the PCH issue. – chris May 31 '13 at 13:53
  • @chris I think the last sentence `but that's how it works in essentially every compiler of which I'm aware` of quotation consolidates his answer. Feel free to correct me if the including quotation is not proper, thanks! – taocp May 31 '13 at 13:55
  • I believe he is referencing the fact that `#include "name"` searches in an implementation-defined manner before falling back to `#include `, but every sane compiler looks in the local directory. – chris May 31 '13 at 13:58
  • @chris OK. I agree. I understand your point now, since the compiler will finally search for headers as used `<>`, then it contradicts my answer. So the real reason might be because when using "", a sane-compiler may stop looking if it cannot find in local directories. Is this true? – taocp May 31 '13 at 14:01
  • Not if it's conforming. The standard mandates it pretend you used angle brackets if not found when using double quotation marks. – chris May 31 '13 at 14:03
  • @chris OK. I guess it would be better to remove the quotations and links. It seems a little weird to me now. The root cause is because `stdafx.h` is a precompiled header and it must be the first include in the file. – taocp May 31 '13 at 14:04
  • @chris I decided to put it there to remind myself and other readers later. I also updated the answer. You are right, it is because PCH problem. Thank you! – taocp May 31 '13 at 14:08
1

If you use #include <iostream> with the <> instead of "" then it should work. Right now, the compiler doesn't know where to find the iostream library.

Also, you might want to change cout<<"example"; to cout<<"example"<<endl; for a new line so that it formats correctly.

  • 2
    If you are just advising to add a new line, then just advise to add a new line: `<< '\n';`. If you are advising to flush also, you should clearly point that out. – BoBTFish May 31 '13 at 13:47
0

Came across this issue while trying to build a Dynamic Linked Library. Make sure that instead of the #include stdafx.h you specify the following include on the first line of your .cpp file:

#include "pch.h"

This should also be the case for VS2017 or earlier.

-2

This error also occurred in the Visual Studio 2017 IDE. Moving stdafx.h to the top solved the error.

For more on stdafx.h, see What's the use for "stdafx.h" in Visual Studio?