Thread: Where to put the "include" directive and "using namespace std" with multiple files?

  1. #1
    Registered User EAX101010's Avatar
    Join Date
    Aug 2014
    Posts
    6

    Where to put the "include" directive and "using namespace std" with multiple files?

    I have two files that I want to compile:

    main.cpp
    Code:
    #include <iostream>
    
    using namespace std;
    
    int ReadNumber();
    void WriteAnswer();
    
    int main()
    {
        WriteAnswer();
        return 0;
    }
    and

    io.cpp
    Code:
    int ReadNumber() {
        int n = 0;
        cin >> n;
        return n;
    }
    void WriteAnswer() {
        cout << ReadNumber() + ReadNumber() << endl;
    }
    The compiler complains:

    io.cpp||In function 'int ReadNumber()':|
    io.cpp|3|error: 'cin' was not declared in this scope|
    io.cpp||In function 'void WriteAnswer()':|
    io.cpp|7|error: 'cout' was not declared in this scope|
    io.cpp|7|error: 'endl' was not declared in this scope|

    In io.cpp file, should I put the two statements ("include <iostream>" and "using namespace std") at the top, outside of the functions?

    Or should I put the two statements inside each of the functions?

    Sorry for the noob question.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I suggest:
    Code:
    // main.cpp
    
    int ReadNumber();
    void WriteAnswer();
    
    int main()
    {
        WriteAnswer();
        return 0;
    }
    Code:
    // io.cpp
    #include <iostream>
    
    using namespace std;
    
    int ReadNumber() {
        int n = 0;
        cin >> n;
        return n;
    }
    
    void WriteAnswer() {
        int n1 = ReadNumber();
        int n2 = ReadNumber();
        cout << (n1 + n2) << endl;
    }
    You might choose to place the forward declarations of ReadNumber and WriteAnswer in a header file. (I note that ReadNumber isn't actually used in main.cpp.)

    Note my changes to WriteAnswer: the order of evaluation is generally unspecified, hence it is better to be clear by moving out the calls to ReadNumber since they have side effects.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    In general, the rule of thumb is:
    If it's a source file, then you can put the using directives at global scope (i.e. outside any functions).
    If it's a header file, then you can put the using directives at function scope (i.e. inside functions). Putting them at local scope is bad practice since it will "poison" any source files that include it.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Registered User Alpo's Avatar
    Join Date
    Apr 2014
    Posts
    877
    Another thing of interest is when and what to include. Here is an article that gives a few easy to follow rules (it has really helped me):

    Headers and Includes: Why and How - C++ Forum

    Basic rules covered :

    - do nothing if: A makes no references at all to B

    - do nothing if: The only reference to B is in a friend declaration

    - forward declare B if: A contains a B pointer or reference: B* myb;

    - forward declare B if: one or more functions has a B object/pointer/reference
    as a parementer, or as a return type: B MyFunction(B myb);

    - #include "b.h" if: B is a parent class of A

    - #include "b.h" if: A contains a B object: B myb;
    Last edited by Alpo; 08-23-2014 at 06:26 PM.

  5. #5
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    By the way, it is generally a bad idea to import such a big namespace wholesale.

    Many beginner tutorials do it for simplicity, but you'll find that almost no serious code does that.

    One of the problems with "using namespace std;" is that the std namespace is huge and always growing.

    For example, it grew quite a bit in C++11 (the new C++ specification).

    If you had older code that did "using namespace std;" and you had a function called "move()", it would have been correct and would have compiled.

    Now if you upgrade your compiler (or compiler setting) to C++11 your code will suddenly break because now there's a std::move(). This is an actual problem I had to fix last week. It was someone else's code.

    I would recommend not using "using namespace std;".

    You can either write std:: all the time, which is what I do and I like it because it serves as a form of documentation, too, to make it immediately clear to the reader that I am referring to something in the standard library (could be hard to tell sometimes in projects with hundreds of source files and thousands of functions).

    Another way is to import specific symbols, and only import the ones you need -
    Code:
    using std::cout;
    using std::endl;
    using std::cin;
    ...
    And as already mentioned, never ever do "using namespace std;" in header files.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 08-19-2012, 06:15 AM
  2. Replies: 3
    Last Post: 04-26-2009, 05:59 AM
  3. "itoa"-"_itoa" , "inp"-"_inp", Why some functions have "
    By L.O.K. in forum Windows Programming
    Replies: 5
    Last Post: 12-08-2002, 08:25 AM
  4. "CWnd"-"HWnd","CBitmap"-"HBitmap"...., What is mean by "
    By L.O.K. in forum Windows Programming
    Replies: 2
    Last Post: 12-04-2002, 07:59 AM