Hello World app not "finding" header files?

I am re-teaching myself Visual C++, and I am starting with the Hello World app (that I think we've all created early in our C++ lives). I get compiler errors that should be covered by the header files I'm including in the project. What am I doing wrong here?


errors:

c:\Documents and Settings\Scott\My Documents\Visual Studio Projects\MyFirstConsoleApp\MyFirstConsoleApp.cpp(6): error C2871: 'std' : a namespace with this name does not exist
c:\Documents and Settings\Scott\My Documents\Visual Studio Projects\MyFirstConsoleApp\MyFirstConsoleApp.cpp(10): error C2065: 'cout' : undeclared identifier
c:\Documents and Settings\Scott\My Documents\Visual Studio Projects\MyFirstConsoleApp\MyFirstConsoleApp.cpp(10): error C2296: '<<' : illegal, left operand has type ''unknown-type''
c:\Documents and Settings\Scott\My Documents\Visual Studio Projects\MyFirstConsoleApp\MyFirstConsoleApp.cpp(10): error C2297: '<<' : illegal, right operand has type 'char [12]'
c:\Documents and Settings\Scott\My Documents\Visual Studio Projects\MyFirstConsoleApp\MyFirstConsoleApp.cpp(10): error C2065: 'endl' : undeclared identifier


code:

// Hello World Application

//

#include "iostream.h"

#include "stdafx.h"

usingnamespace std;

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

{

cout<<"Hello World"<<endl;

return 0;

}


header files included:

iostream.h, istream.h, ostream.h, stdafx.h, stdio.h, tchar.h

[2067 byte] By [rss0213] at [2007-12-25]
# 1
You need to include <iostream> not <iostream.h>
JonathanCaves-MSFT at 2007-9-3 > top of Msdn Tech,Visual C++,Visual C++ General...
# 2

ok, now I have this:

// Hello World Application

//

#include <iostream>

#include "stdafx.h"

using namespace std;

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

{

cout<<"Hello World"<<endl;

return 0;

}


But these errors are generated when I build:

c:\Documents and Settings\Scott\My Documents\Visual Studio Projects\MyFirstConsoleApp\MyFirstConsoleApp.cpp(6): error C2871: 'std' : a namespace with this name does not exist
c:\Documents and Settings\Scott\My Documents\Visual Studio Projects\MyFirstConsoleApp\MyFirstConsoleApp.cpp(10): error C2065: 'cout' : undeclared identifier
c:\Documents and Settings\Scott\My Documents\Visual Studio Projects\MyFirstConsoleApp\MyFirstConsoleApp.cpp(10): error C2296: '<<' : illegal, left operand has type ''unknown-type''
c:\Documents and Settings\Scott\My Documents\Visual Studio Projects\MyFirstConsoleApp\MyFirstConsoleApp.cpp(10): error C2297: '<<' : illegal, right operand has type 'char [12]'
c:\Documents and Settings\Scott\My Documents\Visual Studio Projects\MyFirstConsoleApp\MyFirstConsoleApp.cpp(10): error C2065: 'endl' : undeclared identifier

rss0213 at 2007-9-3 > top of Msdn Tech,Visual C++,Visual C++ General...
# 3

You need to inlcude the header file after the PCH header file otherwise it is ignored. Try:

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

JonathanCaves-MSFT at 2007-9-3 > top of Msdn Tech,Visual C++,Visual C++ General...
# 4
Ok... That worked - now tell me why, please.
rss0213 at 2007-9-3 > top of Msdn Tech,Visual C++,Visual C++ General...
# 5

It is all to do with pre-compiled headers. A pre-compiled header (PCH) is nothing more than a memory dump of the compiler state at certain point in the compilation process. When you reload a pre-compiled header (/Yu) you are basically asking the compiler to restore itself to some previously saved state. This reload is usually (though not always) acheived by using a "marker" header file usually (but again not always) called stdafx.h. So on a compilation in which a pre-compiled header is used the compiler searches for the "marker" header file and when it finds it the compiler reloads the previous state. This, as you have found, has the nasty side-effect of ignoring any #includes, #defines or even code that appears before the "marker" header file.

You'll be glad to hear that in the next release of Visual C++ we have finally, after years of frustrating users like yourself, added a warning which catches most cases were code like a #include is ignored due to the user of a pre-compiled header.

JonathanCaves-MSFT at 2007-9-3 > top of Msdn Tech,Visual C++,Visual C++ General...
# 6
Your reply just ended about 2 hours of painful, hair-pulling frustration. Thanks a million!!
DaveInCali at 2007-9-3 > top of Msdn Tech,Visual C++,Visual C++ General...
# 7
Yep i like this reply!! Thank goodness someone else had this problem before me otherwise i would've been there for hours!! thanks
chrisso909 at 2007-9-3 > top of Msdn Tech,Visual C++,Visual C++ General...
# 8

i am trying to teach myself C++ programing and i'm starting out with the hello world program but when i got to debug the program a command prompt window opens and then closes and nothing happens this is what i have for code and what i get from the debug:

/* My second program in C++ with more comments*/

#include <iostream>

using namespace std;

int main ()

{

cout << "Hello World! "; //prints Hello World!

cout << "This is a C++ Program. "; //prints This is a C++ Program

return 0;

}

'First Program.exe': Loaded 'C:\Documents and Settings\k0474986\My Documents\Visual Studio 2005\Projects\First Program\debug\First Program.exe', Symbols loaded.

'First Program.exe': Loaded 'C:\WINDOWS\system32\ntdll.dll', No symbols loaded.

'First Program.exe': Loaded 'C:\WINDOWS\system32\mscoree.dll', No symbols loaded.

'First Program.exe': Loaded 'C:\WINDOWS\system32\kernel32.dll', No symbols loaded.

'First Program.exe': Loaded 'C:\WINDOWS\system32\advapi32.dll', No symbols loaded.

'First Program.exe': Loaded 'C:\WINDOWS\system32\rpcrt4.dll', No symbols loaded.

'First Program.exe': Loaded 'C:\WINDOWS\WinSxS\x86_Microsoft.VC80.DebugCRT_1fc8b3b9a1e18e3b_8.0.50727.762_x-ww_5490cd9f\msvcp80d.dll', No symbols loaded.

'First Program.exe': Loaded 'C:\WINDOWS\WinSxS\x86_Microsoft.VC80.DebugCRT_1fc8b3b9a1e18e3b_8.0.50727.762_x-ww_5490cd9f\msvcr80d.dll', No symbols loaded.

'First Program.exe': Loaded 'C:\WINDOWS\system32\msvcrt.dll', No symbols loaded.

'First Program.exe': Loaded 'C:\WINDOWS\WinSxS\x86_Microsoft.VC80.DebugCRT_1fc8b3b9a1e18e3b_8.0.50727.762_x-ww_5490cd9f\msvcm80d.dll', No symbols loaded.

'First Program.exe': Loaded 'C:\WINDOWS\system32\ole32.dll', No symbols loaded.

'First Program.exe': Loaded 'C:\WINDOWS\system32\gdi32.dll', No symbols loaded.

'First Program.exe': Loaded 'C:\WINDOWS\system32\user32.dll', No symbols loaded.

'First Program.exe': Loaded 'C:\WINDOWS\system32\imm32.dll', No symbols loaded.

'First Program.exe': Loaded 'C:\WINDOWS\system32\AMInit.dll', Binary was not built with debug information.

'First Program.exe': Loaded 'C:\WINDOWS\system32\shlwapi.dll', No symbols loaded.

'First Program.exe': Loaded 'C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\mscorwks.dll', No symbols loaded.

'First Program.exe': Loaded 'C:\WINDOWS\WinSxS\x86_Microsoft.VC80.CRT_1fc8b3b9a1e18e3b_8.0.50727.762_x-ww_6b128700\msvcr80.dll', No symbols loaded.

'First Program.exe': Loaded 'C:\WINDOWS\system32\shell32.dll', No symbols loaded.

'First Program.exe': Loaded 'C:\WINDOWS\WinSxS\x86_Microsoft.Windows.Common-Controls_6595b64144ccf1df_6.0.2600.2982_x-ww_ac3f9c03\comctl32.dll', No symbols loaded.

'First Program.exe': Loaded 'C:\WINDOWS\system32\comctl32.dll', No symbols loaded.

'First Program.exe': Loaded 'C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\Culture.dll', No symbols loaded.

'First Program.exe': Unloaded 'C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\Culture.dll'

'First Program.exe': Loaded 'C:\WINDOWS\assembly\NativeImages_v2.0.50727_32\mscorlib\0ecf9e01c9ab83448a082672366c5efb\mscorlib.ni.dll', No symbols loaded.

'First Program.exe' (Managed): Loaded 'C:\WINDOWS\assembly\GAC_32\mscorlib\2.0.0.0__b77a5c561934e089\mscorlib.dll', No symbols loaded.

'First Program.exe' (Managed): Loaded 'c:\documents and settings\k0474986\my documents\visual studio 2005\projects\first program\debug\First Program.exe', Symbols loaded.

'First Program.exe': Loaded 'C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\mscorjit.dll', No symbols loaded.

'First Program.exe': Loaded 'C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\diasymreader.dll', No symbols loaded.

'First Program.exe': Loaded 'C:\WINDOWS\system32\rsaenh.dll', No symbols loaded.

'First Program.exe' (Managed): Loaded 'C:\WINDOWS\WinSxS\x86_Microsoft.VC80.DebugCRT_1fc8b3b9a1e18e3b_8.0.50727.762_x-ww_5490cd9f\msvcm80d.dll', No symbols loaded.

'First Program.exe': Loaded 'C:\WINDOWS\assembly\NativeImages_v2.0.50727_32\System\7fb26e715f1f774ca35b26d00d21b4ec\System.ni.dll', No symbols loaded.

'First Program.exe' (Managed): Loaded 'C:\WINDOWS\assembly\GAC_MSIL\System\2.0.0.0__b77a5c561934e089\System.dll', No symbols loaded.

The thread 'Win32 Thread' (0x8e0) has exited with code 0 (0x0).

The thread 'Win32 Thread' (0x7c8) has exited with code 0 (0x0).

The program '[2368] First Program.exe: Managed' has exited with code 0 (0x0).

The program '[2368] First Program.exe: Native' has exited with code 0 (0x0).

PLEASE HELP ME THIS IS ALREADY DRIVING ME NUTS

newbieprogram at 2007-9-3 > top of Msdn Tech,Visual C++,Visual C++ General...
# 9
That actually means it has run fine. The thing is it doesn't keep it open after the program is finished. You have to do one of two things. Either put a call to getch() at the end or run the program without debugging.
To do the start without debugging you either press ctrl+f5 or go to the debug pull down menu and choose start without debugging.

But I wrote a little post on precompiled headers not that long ago, seems as if it has already dropped into oblivion.

crescens2k at 2007-9-3 > top of Msdn Tech,Visual C++,Visual C++ General...