79

I am working on the 'driver' part of my programing assignment and i keep getting this absurd error:

error C2065: 'cout' : undeclared identifier

I have even tried using the std::cout but I get another error that says:

IntelliSense: namespace "std" has no member "cout"

When I have declared using namespace std, included iostream and I even tried to use ostream

#include <iostream>
using namespace std;

int main () {
    cout << "hey" << endl;
 return 0;
}

I'm using Visual Studio 2010 and running Windows 7. All of the .h files have using namespace std and include iostream and ostream.

user438383
  • 5,716
  • 8
  • 28
  • 43
Wallter
  • 4,275
  • 6
  • 29
  • 33
  • 2
    Remove all the header files but iostream, then try again. – Timo Geusch Dec 08 '09 at 17:41
  • 2
    Shouldn't your << end be endl? – RC. Dec 08 '09 at 17:42
  • yeah sorry i changed that but nothing - both the endl and the using only the iostream - tried: using the iostream w/ the namespace std but no luck – Wallter Dec 08 '09 at 17:43
  • 8
    "NOTE: all of the .h files include namespace std - and include io and o streams..." <-- If you mean all of the .h files have "using namespace std;" in them, you might want to reconsider... – Cogwheel Dec 08 '09 at 17:45
  • 3
    `int main`, not `int Main`. And semicolon after a function body is not necessary. – Cat Plus Plus Dec 08 '09 at 17:46
  • 1
    did you try the suggestion given @Timo Geusch? What happens then? – Ponting Dec 08 '09 at 17:46
  • @Ponting - yes-nothing changed – Wallter Dec 08 '09 at 17:49
  • Did you try changing it to int main() and returning a value ? – JonH Dec 08 '09 at 17:50
  • @JonH yes nothing - i tried cleaning the build but still nothing – Wallter Dec 08 '09 at 17:51
  • ok..one more try, what happens when you right click on iostream and use open file iostream (I don't remember what is the exact option)? Is it able to open it? – Ponting Dec 08 '09 at 17:52
  • What compiler are you using? Can you start a new fresh project on a new fresh build of the compiler. Something tells me your files are corrupt or something didn't install so smoothly. Everyone has posted valid C++ code so it should work :). – JonH Dec 08 '09 at 17:54
  • Also repost your entire code as an edit so we can see the changes you made. Otherwise we cannot trust that you've gotten rid of that semi-colon after the bob function. – JonH Dec 08 '09 at 17:55
  • Everything looks good here and compiles but I dont use vs 2010. Isnt that in beta :-p. Argh use 2008 and check it! – JonH Dec 08 '09 at 17:59
  • yeah i think it's the compiler, IDK why it would do this but i'm going to reinstall :) thanks guys – Wallter Dec 08 '09 at 18:08
  • 1
    Did you actually try *compiling* this or did you just see errors in Visual Studio? I believe the first time you start a C++ project in VS2010, it may start scanning all the `std` headers and may not identify even basic stuff like `std::cout` at first. But if you try building it, it should work out... – Dan Tao Dec 08 '09 at 18:24
  • Also, is this an empty project, or a CLR project? I'm pretty sure Intellisense doesn't work in VS2010 for CLR (i.e., .NET) projects. – Dan Tao Dec 08 '09 at 18:32
  • Intellisense doesn't work for C++/CLI, but he shouldn't be getting any error messages regardless (just no completion prompts). – Pavel Minaev Dec 08 '09 at 18:39
  • Your question only mentions intellisense. Do you get actual *compile errors* too? – jalf Dec 08 '09 at 18:45
  • 1
    Jalf, the very first paragraph mentions compiler error C2065. – Rob Kennedy Dec 08 '09 at 19:19
  • What is the name of the file you're trying to compile, including extension? I've found that Visual Studio will compile anything with a .c extension as a c file and .cpp with c++... Sounds stupid I know, and probably already suggested. – Todd Richardson Dec 08 '09 at 21:25
  • 14
    I seriously doubt Visual Studio has a bug concerning cout. – GManNickG Dec 15 '09 at 03:30
  • It bewildered me as I faced the exact same problem and I started looking for an explanation online, which is why I am here. I am inclined to think the compiler did not get it right at first when I used this line, std::cout<<"Hi"; But after I did a build and used it, the error goes away. Also I included this line, "#include <%iostream%>" without the '%' symbol in stdafx.h. Had to use % here since the browser was parsing that as a tag. I hope you have already found a solution by now! :) –  Feb 21 '11 at 10:47
  • You're running it as a C, not C++. – Lightness Races in Orbit Dec 07 '12 at 09:15
  • 1
    @Wallter If there is a bug, it would be great if you could describe the bug in your answer and include a link to the bug report. – jogojapan Dec 17 '12 at 01:31
  • Why has this question got so many votes? OOB this code works and there is no definitive answer to this.. – paulm Feb 02 '15 at 17:41

26 Answers26

69

In Visual Studio you must #include "stdafx.h" and be the first include of the cpp file. For instance:

These will not work.

#include <iostream>
using namespace std;
int main () {
    cout << "hey" << endl;
    return 0;
}




#include <iostream>
#include "stdafx.h"
using namespace std;
int main () {
    cout << "hey" << endl;
    return 0;
}

This will do.

#include "stdafx.h"
#include <iostream>
using namespace std;
int main () {
    cout << "hey" << endl;
    return 0;
}

Here is a great answer on what the stdafx.h header does.

George Chondrompilas
  • 3,167
  • 27
  • 35
  • 3
    This should be the correct answer since it specifies why the code works. Thanks a lot. – Eliud Jan 23 '17 at 21:47
  • 1
    Yeah, this on should be the accepted answer. Most of the others work as well, but this states it very clearly. @Wallter – Konstantin A. Magg Jun 12 '17 at 13:09
  • 1
    I just singed in to cast a vote. This is the answer, stated clearly. – Samir Jul 19 '17 at 22:33
  • Except nobody is mentioning what `stdafx.h` does. – uitty400 Jun 10 '18 at 08:33
  • This actually isn't right. You only have to do that if the project is configured to use precompiled headers. If the project is NOT configured to use precompiled headers, just including is fine. – George Dec 15 '22 at 18:22
48

write this code, it works perfectly..

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

using namespace std;

int main()
{
 cout<<"Hello World!";
  return 0;
}
Hrishikesh Choudhari
  • 11,617
  • 18
  • 61
  • 74
11

I had same problem on Visual Studio C++ 2010. It's easy to fix. Above the main() function just replace the standard include lines with this below but with the pound symbol in front of the includes.

# include "stdafx.h"
# include <iostream>
using  namespace std;
Bo Persson
  • 90,663
  • 31
  • 146
  • 203
mee
  • 119
  • 1
  • 3
10

The include "stdafx.h" is ok

But you can't use cout unless you have included using namespace std

If you have not included namespace std you have to write std::cout instead of simple cout

Yu Hao
  • 119,891
  • 44
  • 235
  • 294
  • This is the correct answer. You need to use `using namespace std' where cout resides. In modern C++, all functions are within is own namespace. – TheTechGuy Mar 14 '20 at 18:15
3

If the only file you include is iostream and it still says undefined, then maybe iostream doesn't contain what it's supposed to. Is it possible that you have an empty file coincidentally named "iostream" in your project?

Bob Lied
  • 601
  • 4
  • 11
3

I've seen similar things happen when I was using the .c file extension with C++ code. Other than that, I'd have to agree with everyone about a buggy installation. Does it work if you try to compile the project with an earlier release of VS? Try VC++ Express 2008. Its free on msdn.

Yu Hao
  • 119,891
  • 44
  • 235
  • 294
goatlinks
  • 761
  • 1
  • 4
  • 10
3

I have seen that if you use

#include <iostream.h>

then you will get the problem.

If you use

#include <iostream>  

(notice - without the .h)

then you will not get the problem you mentioned.

sth
  • 222,467
  • 53
  • 283
  • 367
G B
  • 39
  • 1
3

If you started a project requiring the #include "stdafx.h" line, put it first.

Jerrey
  • 39
  • 1
3

Such a silly solution in my case:

// Example a
#include <iostream>    
#include "stdafx.h"

The above was odered as per example a, when I changed it to resemble example b below...

// Example b
#include "stdafx.h"
#include <iostream>  

My code compiled like a charm. Try it, guaranteed to work.

Sizons
  • 640
  • 2
  • 8
  • 24
2

before you begin this program get rid of all the code and do a simple hello world inside of main. Only include iostream and using namespace std;. Little by little add to it to find your issue.

cout << "hi" << endl;
derekerdmann
  • 17,696
  • 11
  • 76
  • 110
JonH
  • 32,732
  • 12
  • 87
  • 145
  • i just did but nothing changed – Wallter Dec 08 '09 at 17:44
  • Ok so do what everyone else said. Get rid of all your code. just keep the using namespace and the header iostream. then put this in: int main(void) { std::cout << "Hello, World!" << std::endl; return 0; } tell us if that works. – JonH Dec 08 '09 at 17:48
  • 1
    If it won't compile a file with just `#include int main() { std::cout << "ok"; }`, then something is badly broken in your C++ implementation. What compiler are you using? – Pavel Minaev Dec 08 '09 at 17:48
  • Also it's main not Main. Case is important in C++. I suggest you google C++ hello world. – JonH Dec 08 '09 at 17:49
  • Get rid of that semi colon after the function bob. It comes before main so you do not need it. – JonH Dec 08 '09 at 17:52
  • I don't think VS 2010 Beta 2 is usable for C++ yet. There's some serious problems with the headers. I can't compile anything I've got on it. – David Thornley Dec 08 '09 at 18:03
  • @David: that would be extremely strange, as I use VS2010 beta 2 quite a lot, specifically for C++, without any problems. If you have some serious troubles, please report it as a bug at MS Connect! (https://connect.microsoft.com/VisualStudio/feedback/) – Pavel Minaev Dec 08 '09 at 18:07
  • @Wallter: are you compiling this from VS? or from command line using `cl.exe`? – Pavel Minaev Dec 08 '09 at 18:08
  • @Pavel Minaev: using the built in CTRL-F5 build all feature – Wallter Dec 15 '09 at 03:31
2

The code below compiles and runs properly for me using gcc. Try copy/pasting this and see if it works.

#include <iostream>
using namespace std;

int bob (int a) { cout << "hey" << endl; return 0; };

int main () {
    int a = 1;
    bob(a);
    return 0;
}
Glen
  • 21,816
  • 3
  • 61
  • 76
  • yeah same thing - Error 1 error C2065: 'cout' : undeclared identifier && IntelliSense: identifier "cout" is undefined – Wallter Dec 08 '09 at 17:53
  • Does std::cout work? I don't know much about Visual Studio, are you sure you've set the project up correctly as a C++ project? If you open the file iostream does it contain a cout declaration? – Glen Dec 08 '09 at 17:58
  • Given that VS2010 is still beta it might be a bug (which beta version are you using?). But I find it hard to believe that something so basic doesn't work correctly. – Glen Dec 08 '09 at 18:02
  • on ubuntu C++ project; adding this "using namespace std;" after the "#include " did the works - thanks – serup Oct 25 '16 at 07:07
2

I have VS2010, Beta 1 and Beta 2 (one on my work machine and one at home), and I've used std plenty without issues. Try typing:

std::

And see if Intellisense gives you anything. If it gives you the usual stuff (abort, abs, acos, etc.), except for cout, well then, that is quite a puzzler. Definitely look into your C++ headers in that case.

Beyond that, I would just add to make sure you're running a regular, empty project (not CLR, where Intellisense is crippled), and that you've actually attempted to build the project at least once. As I mentioned in a comment, VS2010 parses files once you've added an include; it could be that something stuck the parser and it didn't "find" cout right away. (In which case, try restarting VS maybe?)

Dan Tao
  • 125,917
  • 54
  • 300
  • 447
2

I had the same issue when starting a ms c++ 2010 project from scratch - I removed all of the header files generated by ms and but used:

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

int main() {
   cout << "hey" << endl;
   return 0;
}

I had to include stdafx.h as it caused an error not having it in.

sth
  • 222,467
  • 53
  • 283
  • 367
oly
  • 21
  • 1
2

Try it, it will work. I checked it in Windows XP, Visual Studio 2010 Express.

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

void main( ) 
{
   int i = 0;
   cout << "Enter a number: ";
   cin >> i;
}
bluish
  • 26,356
  • 27
  • 122
  • 180
2

Take the code

#include <iostream>
using namespace std;

out of your .cpp file, create a header file and put this in the .h file. Then add

#include "whatever your header file is named.h"

at the top of your .cpp code. Then run it again.

sth
  • 222,467
  • 53
  • 283
  • 367
Jameyson
  • 21
  • 1
1

When you created your project, you did not set 'use precompiled headers' correctly. Change it in properties->C/C++->precompiled headers.

john k
  • 6,268
  • 4
  • 55
  • 59
1

In Visual studio use all your header filer below "stdafx.h".

Nithin
  • 11
  • 1
1

Are you sure it's compiling as C++? Check your file name (it should end in .cpp). Check your project settings.

There's simply nothing wrong with your program, and cout is in namespace std. Your installation of VS 2010 Beta 2 is defective, and I don't think it's just your installation.

I don't think VS 2010 is ready for C++ yet. The standard "Hello, World" program didn't work on Beta 1. I just tried creating a test Win32 console application, and the generated test.cpp file didn't have a main() function.

I've got a really, really bad feeling about VS 2010.

David Thornley
  • 56,304
  • 9
  • 91
  • 158
  • 2
    Given that VS2010 is written using itself (and compiled using VC10 compiler, and the code uses new C++0x features such as lambdas, etc), I can assure you that it's quite ready for C++. If you have problems with "Hello, world" applications, then it sounds like a broken install (which is likely a bug), or some post-install bug. Either way it's not normal, and it's definitely not a normal experience in either beta 1 or beta 2, so please report it as a bug on Connect. – Pavel Minaev Dec 08 '09 at 18:32
  • @Pavel: Thanks; as you can tell, I haven't been getting it to work. Sometime after this current crisis I'll try to get some bug reports in. – David Thornley Dec 08 '09 at 21:37
1

Just use printf!

Include stdio.h in your stdafx.h header file for printf.

Paresh J
  • 2,401
  • 3
  • 24
  • 31
Ngoo Nam
  • 27
  • 1
1

Include the std library by inserting the following line at the top of your code:

using namespace std;
WonderWorker
  • 8,539
  • 4
  • 63
  • 74
cahit beyaz
  • 4,829
  • 1
  • 30
  • 25
0

is normally stored in the C:\Program Files\Microsoft Visual Studio 8\VC\include folder. First check if it is still there. Then choose Tools + Options, Projects and Solutions, VC++ Directories, choose "Include files" in the "Show Directories for" combobox and double-check that $(VCInstallDir)include is on top of the list.

0

I came here because I had the same problem, but when I did #include "stdafx.h" it said it did not find that file.
What did the trick for me was: #include <algorithm>.
I use Microsoft Visual Studio 2008.
These are the things that you can use then, incl. 'count': Link

Tenzin
  • 2,415
  • 2
  • 23
  • 36
0

Had this problem, when header files declared "using namespace std;", seems to be confusing for GNU compiler; anyway is bad style!

Solution was providing std::cout ... in headers and moving "using namespace std" to the implementation file.

Sam Ginrich
  • 661
  • 6
  • 7
0

I ran across this error after just having installed vs 2010 and just trying to get a nearly identical program to work.

I've done vanilla C coding on unix-style boxes before, decided I'd play with this a bit myself.

The first program I tried was:

#include "stdafx.h"


int _tmain(int argc, _TCHAR* argv[])
{
    cout << "Hello World!";
    return 0;
}

The big thing to notice here... if you've EVER done any C coding,

int _tmain(int argc, _TCHAR* argv[])

Looks weird. it should be:

int main( int argc, char ** argv )

In my case I just changed the program to:

#include <iostream>
using namespace std;

int main()
{
     cout << "Hello world from  VS 2010!\n";
     return 0;
}

And it worked fine.

Note: Use CTRL + F5 so that the console window sticks around so you can see the results.

avgvstvs
  • 6,196
  • 6
  • 43
  • 74
-1

In VS2017, stdafx.h seems to be replaced by pch.h see this article,

so use:

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

using namespace std;

int main() {
    cout << "Enter 2 numbers:" << endl;
yu yang Jian
  • 6,680
  • 7
  • 55
  • 80
-5

It was the compiler - I'm now using Eclipse Galileo and the program works like a wonder


Wallter
  • 4,275
  • 6
  • 29
  • 33
  • It was the compiler, at the time Visual studio was in the Beta stage, this was one of the bugs that they would later debug out of the compiler. I am now using the official release of the the Visual Studio 2010 compiler and all is working. There is no question that the compiler was the problem. (as stated below by Amelia - I was not alone in this problem) – Wallter Sep 23 '10 at 20:29
  • 1
    Except that Amelia talks about a different compiler (which *definitely* doesn't have this problem). Btw, it' be nice if you edited the question to specifically mention that the problem was related to the 2010 *beta*. Would be a shame if some newbie writes bad code, then sees your question and assumes that it's VS2010 that's just broken. – jalf Dec 29 '10 at 09:19