dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 43

Thread: Namespace and stdafx.h

Hybrid View

  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,716

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


  4. #4
    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.

  5. #5
    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!

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

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

  8. #8
    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.

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

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

  13. #13
    Join Date
    Aug 2002
    Location
    Cluj-Napoca,Romania
    Posts
    3,496

    Re: Namespace and stdafx.h

    Quote Originally Posted by Xeon
    Maybe I gotta skip this for now or else I'll stress out myself.
    At some point you'll have enough skipped things and you'll have to deal with them all.

    Postponning things is pretty similar with how the flush works. From time to time the stream has to deal with all the information it collected but not yet written on the physical device.

    Hope it helped.
    Har Har

  14. #14
    Join Date
    Apr 2019
    Posts
    1

    Unhappy [Error] iostream.h: No such file or directory

    Code:
    #include<iostream.h> 
    \\ Esta  directiva permite usar la funcion de lectura cin y la funcion de escritura cout propias del c++
    using namespace std;  
     \\Localizar el nombre de identificadores para evitar colisiones tener compatibilidad cuando un programa en C++,incluye una libreria de C
    int main()
    {
     int a, b;            
      \\ declara el tipo de dato de a y b
     cout << "Ingresa el primer numero"<<endl; \\ la funcion cout, directiva de escritura,espera el valor del tipo de a
     cin >> a;              \\la  funcion cin, directiva de lectura
     std cout << "Ingresa el segundo numero"<<endl; \\la funcion cout, directiva de escritura
     cin >> b;              \\la  funcion cin, directiva de lectura
     cout <<"La suma de los numeros es: "<< a+b <<endl;\\la funcion cout, directiva de escritura
     cout <<"La resta de los numeros es: "<< a-b <<endl; // 
     la funcion cout, directiva de escritura
     cout <<"La multiplicacion de los numeros es: "<< a*b <<endl;\\la funcion cout, directiva de escritura
     cout <<"La division de los numeros es: "<< a/b <<endl;\\la funcion cout, directiva de escritura
     cout <<"El residuo es: "<< a%b <<endl;\\la funcion cout, directiva de escritura
    
     system("pause");\\
     return EXIT_SUCCESS;\\
    }
    buenas noches,saludos a todos gurus de C++
    he comenzado un curso en c++
    he aqui el primer traspies
    esta es la tarea, he buscado en la red y ya me duele la cabeza, dicen que no use el .h, pero si le quito la h a iostream, me genera mas errores como [Error] stray '\' in program, por favor su ayuda
    gracias
    Last edited by 2kaud; April 16th, 2019 at 03:52 AM. Reason: Added code tags

  15. #15
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,155

    Re: [Error] iostream.h: No such file or directory

    Quote Originally Posted by popayan563 View Post
    Code:
    #include<iostream.h> 
    \\ Esta  directiva permite usar la funcion de lectura cin y la funcion de escritura cout propias del c++
    using namespace std;  
     \\Localizar el nombre de identificadores para evitar colisiones tener compatibilidad cuando un programa en C++,incluye una libreria de C
    int main()
    {
     int a, b;            
      \\ declara el tipo de dato de a y b
     cout << "Ingresa el primer numero"<<endl; \\ la funcion cout, directiva de escritura,espera el valor del tipo de a
     cin >> a;              \\la  funcion cin, directiva de lectura
     std cout << "Ingresa el segundo numero"<<endl; \\la funcion cout, directiva de escritura
     cin >> b;              \\la  funcion cin, directiva de lectura
     cout <<"La suma de los numeros es: "<< a+b <<endl;\\la funcion cout, directiva de escritura
     cout <<"La resta de los numeros es: "<< a-b <<endl; // 
     la funcion cout, directiva de escritura
     cout <<"La multiplicacion de los numeros es: "<< a*b <<endl;\\la funcion cout, directiva de escritura
     cout <<"La division de los numeros es: "<< a/b <<endl;\\la funcion cout, directiva de escritura
     cout <<"El residuo es: "<< a%b <<endl;\\la funcion cout, directiva de escritura
    
     system("pause");\\
     return EXIT_SUCCESS;\\
    }
    buenas noches,saludos a todos gurus de C++
    he comenzado un curso en c++
    he aqui el primer traspies
    esta es la tarea, he buscado en la red y ya me duele la cabeza, dicen que no use el .h, pero si le quito la h a iostream, me genera mas errores como [Error] stray '\' in program, por favor su ayuda
    gracias
    This is an English only forum. Please post only in English.

Page 1 of 2 12 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
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)