-
November 1st, 2005, 10:01 AM
#1
Namespace and stdafx.h
Hi all,
I'm using Visual C++ 6.0 and reading from a very new C++ book written in 2005.
Below is my code from the book :
Code:
#include <iostream.h>
#include <stdafx.h>
using namespace std;
int main()
{
cout << "Never fear, C++ is here!" << endl;
cout << "Do you C++?" << endl;
cout << "I'm back!\n";
cout << "I love real women!\n";
return 0;
}
However, the compile gave me an error :
C:\C++\Test\Test.cpp(3) : fatal error C1083: Cannot open include file: 'stdafx.h': No such file or directory
Obviously, if I remove "#include <stdafx.h>" and "using namespace std;", the error disappears, but what I like to know if why this error appeared in the 1st place. How can there be no "stdafx.h" file in Visual C++? Also, "using namespace std;" is a valid statement, right?
(I know the program can run without these 2 lines, though)
Is it because Visual C++ 6.0 is too old?
Thanks!
Xeon.
Last edited by Xeon; November 1st, 2005 at 10:05 AM.
"Hell is calling for you!" - Rufus, from Valkyrie Profile 2 : Silmeria
"I'm getting tired of you devils.....finishing strike......Final Blast!" - Arngrim, from Valkyrie Profile 2 : Silmeria
-
November 1st, 2005, 10:10 AM
#2
Re: Namespace and stdafx.h
1) I don't see why you need stdafx.h in a console app
2). your book is old if it uses <iostream.h> , it should be <iostream>
(or maybe you added the .h ?)
3) corrected code
Code:
#include <iostream>
using namespace std;
int main()
{
cout << "Never fear, C++ is here!" << endl;
cout << "Do you C++?" << endl;
cout << "I'm back!\n";
cout << "I love real women!\n";
return 0;
}
-
November 1st, 2005, 10:11 AM
#3
Re: Namespace and stdafx.h
The Visual Studio environment creates a precompiled header file per default which is named stdafx. A precompiled header is not needed to compile this, but it is created by the IDE for default. If you don't have it, or don't want to use precompiled header files remove the reference to stdafx header file. For more informations on precompiled header files see this link on MSDN.
-
November 1st, 2005, 11:00 AM
#4
Re: Namespace and stdafx.h
use double quotes, not angle brackets for files located in the project directory.
Code:
#include "stdafx.h"
VC++ 6.0 is a very very old compiler that predates current c++ standards. So if you are using a new book that teaches C99 standards then VC++ 6.0 may not be a good compiler for you to use. I'd suggest Dev-C++.
-
November 1st, 2005, 11:09 AM
#5
Re: Namespace and stdafx.h
Ah, a technical thread from Xeon.
How can there be no "stdafx.h" file in Visual C++?
stdafx.h is not a file that belongs to the VC++ environment. It's a file that belongs to the project. It is generated by default, and used for precompiled headers. You should not include it between <> but "". You can look in your project folder and search for it. You can choose not to use procompiled headers, from Project >> Settings >> C/C++ >> Preompiled Headers. Notice that the file can have any name, this is just the default name used by VC++ when generating the header.
About the C++ headers, see this FAQ.
About this:
Code:
cout << "Never fear, C++ is here!" << endl;
cout << "Do you C++?" << endl;
notice that each time you call endl function, the buffers are flushed. This may prove expensive some times, so instead of std::endl you could put a '\n'.
-
November 1st, 2005, 11:16 AM
#6
Re: Namespace and stdafx.h
Originally Posted by stober
So if you are using a new book that teaches C99 standards then VC++ 6.0 may not be a good compiler for you to use.
If that code is taken from the book, it is definitelly written in VC++ (6.0 or 7.x). "stdafx.h" is Microsoft specific. I don't think Dev-C++ generates that file.
Why each time somebody uses the word "C++", people jump replying with the word "standard"? Why do you always assume people need portable code? Why is VC++ such a non-grata compiler? It works great for me!!!
-
November 1st, 2005, 11:24 AM
#7
Re: Namespace and stdafx.h
Originally Posted by Philip Nicoletti
1) I don't see why you need stdafx.h in a console app
2). your book is old if it uses <iostream.h> , it should be <iostream>
(or maybe you added the .h ?)
Something else makes me very suspicious of the credentials of the author: iostream.h is included above stdafx.h. As everyone who's used VC++ for more than 5 minutes knows, it ignores all source until it hits the stdafx.h include statement.
Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
-- Sutter and Alexandrescu, C++ Coding Standards
Programs must be written for people to read, and only incidentally for machines to execute.
-- Harold Abelson and Gerald Jay Sussman
The cheapest, fastest and most reliable components of a computer system are those that aren't there.
-- Gordon Bell
-
November 1st, 2005, 11:34 AM
#8
Re: Namespace and stdafx.h
Originally Posted by Graham
Something else makes me very suspicious of the credentials of the author: iostream.h is included above stdafx.h. As everyone who's used VC++ for more than 5 minutes knows, it ignores all source until it hits the stdafx.h include statement.
Ah, that is a good catch Graham. It fuels my fears that Xeon is playing a joke on us...
-
November 1st, 2005, 11:58 AM
#9
Re: Namespace and stdafx.h
Originally Posted by cilu
Why each time somebody uses the word "C++", people jump replying with the word "standard"? Why do you always assume people need portable code? Why is VC++ such a non-grata compiler? It works great for me!!!
It worked great for you -- good for you. But try using some of the c++ features that are new to c++ and you will find VC++ 6.0 and even VC++ 7.0 can't (won't) do them. People just learning the language (i.e. students) have a hard enough time without the complications of learning current c++ standards with ancient compilers.
Last edited by stober; November 1st, 2005 at 12:08 PM.
-
November 1st, 2005, 04:50 PM
#10
Re: Namespace and stdafx.h
Originally Posted by stober
It worked great for you -- good for you. But try using some of the c++ features that are new to c++ and you will find VC++ 6.0 and even VC++ 7.0 can't (won't) do them. People just learning the language (i.e. students) have a hard enough time without the complications of learning current c++ standards with ancient compilers.
Yes, I am aware of the compatibility issues with the C++ standard of VC++ 6.0 compiler. However, if you read carefully the first post, you will see it's about VC++ 6.0.
-
November 1st, 2005, 05:48 PM
#11
Re: Namespace and stdafx.h
Originally Posted by cilu
Yes, I am aware of the compatibility issues with the C++ standard of VC++ 6.0 compiler. However, if you read carefully the first post, you will see it's about VC++ 6.0.
which is why I made that comment about using a different compiler. Terrific compiler to write MS-Windows programs, but pretty terrible for learning the C and/or C++ languages. There are alot better compilers for that purpose which the OP and other beginners should use.
-
November 1st, 2005, 11:22 PM
#12
Re: Namespace and stdafx.h
"Hell is calling for you!" - Rufus, from Valkyrie Profile 2 : Silmeria
"I'm getting tired of you devils.....finishing strike......Final Blast!" - Arngrim, from Valkyrie Profile 2 : Silmeria
-
November 1st, 2005, 11:41 PM
#13
Re: Namespace and stdafx.h
reading about that book on amazon.com it appears to be adequate introduction to c++ language. But this sentence bothers me
The accompanying CD-ROM contains a free C++ compiler for writing and running C++ programs, which will let you get started right away. It also includes all the examples and answers to all the exercises in the book. The CD-ROM will run on any PC running MS-DOS or Windows.
I assume the compiler is VC++ 6.0 -- which will NOT run in MS-DOS operating system.
-
November 2nd, 2005, 02:26 AM
#14
Re: Namespace and stdafx.h
Originally Posted by cilu
notice that each time you call endl function, the buffers are flushed. This may prove expensive some times, so instead of std::endl you could put a '\n'.
...also, note that std::cout is un-buffered by default and hence std::endl won't cause a flush everytime. It won't be an issue under the default settings. This might be a concern in case the default behaviour is changed or when you are using buffered ones like Files streams. For details: have a look at this thread - At what time i should use cout.flush() . Regards.
Can you help me with my homework assignment?, Before you post!, Use code tags, How to post!, Codeguru technical FAQs, C++ FAQ Lite, Stroustrup: C++ Style and Technique FAQ, Guru of the Week, Comeau C and C++ FAQs, Comeau C++ Templates FAQs, CUJ @ DDJ, Spam threshold
My Blogs : Learning C++ is fun | Abnegator's reflections
Open Threads : C++ Aha! Moments | Nature of work in C++?
-
November 2nd, 2005, 08:25 AM
#15
Re: Namespace and stdafx.h
"Hell is calling for you!" - Rufus, from Valkyrie Profile 2 : Silmeria
"I'm getting tired of you devils.....finishing strike......Final Blast!" - Arngrim, from Valkyrie Profile 2 : Silmeria
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|