CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 3 123 LastLast
Results 1 to 15 of 43
  1. #1
    Xeon's Avatar
    Xeon is offline Want me to ban you?! Power Poster
    Join Date
    Jul 2000
    Location
    Singapore
    Posts
    4,195

    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

  2. #2
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,721

    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;
    }

  3. #3
    Join Date
    Mar 2004
    Location
    (Upper-) Austria
    Posts
    2,899

    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.
    I am not offering technical guidiance via email or IM
    Come on share your photo with us! CG members photo album!
    Use the Code Tags!

  4. #4
    Join Date
    Jun 2002
    Posts
    1,417

    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++.

  5. #5
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    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'.
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  6. #6
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    Re: Namespace and stdafx.h

    Quote 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!!!
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  7. #7
    Join Date
    Apr 1999
    Location
    Altrincham, England
    Posts
    4,470

    Re: Namespace and stdafx.h

    Quote 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


  8. #8
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    Re: Namespace and stdafx.h

    Quote 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...
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  9. #9
    Join Date
    Jun 2002
    Posts
    1,417

    Re: Namespace and stdafx.h

    Quote 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.

  10. #10
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    Re: Namespace and stdafx.h

    Quote 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.
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  11. #11
    Join Date
    Jun 2002
    Posts
    1,417

    Re: Namespace and stdafx.h

    Quote 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.

  12. #12
    Xeon's Avatar
    Xeon is offline Want me to ban you?! Power Poster
    Join Date
    Jul 2000
    Location
    Singapore
    Posts
    4,195

    Re: Namespace and stdafx.h

    From 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.
    Nah, maybe it's a wrong positioning of code on my part. I tend to play around with code here and there to exercise and get my hands dirty.

    The book I'm using is called "C++ Without Fear : A Beginner's Guide That Makes You Feel Smart" by Brian Overland, a programmer at Microsoft for 10 years. It's here :


    Pretty cute, eh? Nice starting point to get re-aquainted with C++.

    Good day and thanks for your replies, all!
    Xeon.
    "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

  13. #13
    Join Date
    Jun 2002
    Posts
    1,417

    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.

  14. #14
    Join Date
    Feb 2005
    Location
    "The Capital"
    Posts
    5,306

    Thumbs up Re: Namespace and stdafx.h

    Quote 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.

  15. #15
    Xeon's Avatar
    Xeon is offline Want me to ban you?! Power Poster
    Join Date
    Jul 2000
    Location
    Singapore
    Posts
    4,195

    Re: Namespace and stdafx.h

    From Stober:
    I assume the compiler is VC++ 6.0 -- which will NOT run in MS-DOS operating system.
    Nah pal, the compiler comes with the book, so how can they possibly give you Visual C++ 6.0(which costs hundreds of dollars) when the book is less than US$35? Hello???!!!

    The compiler that came with this book is the RHIDE development enviroment. Dunno if you've heard of it before.

    By the way, guys.....I really don't understand what is buffer flusing. Maybe I gotta skip this for now or else I'll stress out myself.

    Thanks all!
    Xeon.
    "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

Page 1 of 3 123 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured