Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Include header files order?

  1. Oct 29, 2007 #1
    Is there a certain order that you have to include header files? I have an already error free program in c++, but when i insert some code to change the background and foreground of the console application it gives me errors. It's when i include the windows.h I get the errors. When i comment out the color change code and the windows.h line it compiles with NO errors. The errors I'm getting is:

    Test6.cpp(146) : error C2065: 'numeric_limits' : undeclared identifier
    Test6.cpp(146) : error C2062: type 'int' unexpected
    Test6.cpp(146) : warning C4003: not enough actual parameters for macro 'max'
    Test6.cpp(146) : error C2589: '(' : illegal token on right side of '::'
    Test6.cpp(155) : error C2062: type 'int' unexpected
    Test6.cpp(155) : warning C4003: not enough actual parameters for macro 'max'
    Test6.cpp(155) : error C2589: '(' : illegal token on right side of '::'
    Test6.cpp(155) : error C3861: 'numeric_limits': identifier not found, even with argument-dependent lookup


    It's from these lines of code in my program which is just input validation:

    bool numbercheck(int& integer)
    {
    if (cin)
    {
    cin.ignore(numeric_limits<int>::max(), '\n');
    if (cin.gcount() == 1)
    wrong = true;
    //cout << "YOU GOT IT RIGHT!" << endl;
    return true;
    }
    else
    {
    cin.clear();
    cin.ignore(numeric_limits<int>::max(), '\n');
    cout << "\n\nWRONG CHARACTER! TRY AGAIN......" << endl;
    cout << endl;
    return false;
    }
    }


    When i comment these 2 lines out: cin.ignore(numeric_limits<int>::max(), '\n'); and leave the color change code and windows.h in, it compiles with NO errors. I need those 2 lines of code for my program to validate correctly. Do I have to insert the color change code and the "#include <windows.h>" in a certain part of my code for them to work together?
    Thanx in Advance!!!
     
  2. jcsd
  3. Oct 29, 2007 #2

    KTC

    User Avatar

    You haven't really provided enough information for us to provide good answer. You're asking about inclusion order, but for example haven't actually show what your current order is.

    But generally speaking, '#include <windows.h>' comes before all the other includes.

    Looking at the errors, you haven't included the right headers for what you're using. numeric_limits is declared in <limits>.
     
  4. Oct 29, 2007 #3
    Ooops! Sorry! Here are my header files:
    #include <stdafx.h>
    #include "iostream"
    #include <limits>
    #include <stdio.h>
    #include <stdlib.h>
    #include <conio.h>
    #include <iomanip>
    #include <cmath>
    #include <fstream>
    #include <string.h>


    A professor gave me these headers to use as a template for commonly used headers.
     
    Last edited: Oct 29, 2007
  5. Oct 29, 2007 #4

    rcgldr

    User Avatar
    Homework Helper

    Do you have a "using namespace std" in your code?

    If not, then the syntax is "std::cin", "std::cout", ...
     
  6. Oct 29, 2007 #5

    jtbell

    User Avatar

    Staff: Mentor

    Ugh! I wonder why your professor is mixing standard C++ headers with earlier non-standard ones? And "iostream" should have angle brackets instead. I would use

    Code (Text):

    #include <iostream>
    #include <limits>
    #include <cstdio>
    #include <cstdlib>
    #include <conio.h>
    #include <iomanip>
    #include <cmath>
    #include <fstream>
    #include <cstring>

    using namespace std;
     
    <conio.h> is a Windows-specific header, but all the other headers have standard C++ versions which have existed for about ten years now.
     
  7. Oct 29, 2007 #6

    Dr Transport

    User Avatar
    Science Advisor
    Gold Member

    If I remember correctly, if the header is in quotes, the compiler is supposed to look in the working directory, not the standard headers directory for the file, just in case someone has rewritten the header file.
     
  8. Oct 29, 2007 #7
    Yes! "using namespace std" is included after all of the "include" statements. And "iostream" is actually in brackets. I was just trying everything possible to find a solution. Where should insert the "windows.h"?
     
  9. Oct 30, 2007 #8

    KTC

    User Avatar

    No, "header" works as well.


    Code (Text):
    #include <stdafx.h>
    <stdafx.h> is a MSVC++ specific header. In particular, you need to include it if you're using precompiled header, and if that's the case, you need to include it before everything else.

    Code (Text):
    #include <stdio.h> // or <cstdio>
    Oh, please don't mix and match C and C++ IO. Use one or the other in your program.

    Code (Text):
    #include <conio.h>
    <conio.h> is not only windows specific, it's old, as in from and for back in the days of DOS. There's no good reason why you need to use it at all.

    Code (Text):
    #include <cstring> // or <cstring>
    Make sure you're including this for the character manipulation functions, and not for C++ style string from <string>. And yes, do use C++ style std::string instead of C style character arrays string.

    Before every other header file inclusion, except that of <stdafx.h> if you're using precompiled header on MSVC++.

    Code (Text):
    #include <windows.h>

    #include <iostream>
    #include <fstream>
    #include <limits>
    #include <iomanip>

    #include <cstdlib>
    #include <cmath>
    #include <cstring>
    Oh, there's really no reason to include all those (and others like <vector>, <string>, and <algorithm>, ...) headers unless you're actually using something from it.
     
  10. Oct 30, 2007 #9
    @ KTC:
    Thanks a lot for going through all of that trouble with your reply. I guess, honestly, I don't really understand the concept of header files so that's why i include so many. That's why a couple of weeks ago i asked where could i find all of the header files available for MSVC++ just in case I'm using something that might need a certain header. Per your instructions I included the "windows.h", but I keep getting this compile error:
    test5.cpp(149) : error C2065: 'numeric_limits' : undeclared identifier
    test5.cpp(149) : error C2062: type 'int' unexpected
    test5.cpp(149) : warning C4003: not enough actual parameters for macro 'max'
    test5.cpp(149) : error C2589: '(' : illegal token on right side of '::'
    test5.cpp(158) : error C2062: type 'int' unexpected
    test5.cpp(158) : warning C4003: not enough actual parameters for macro 'max'
    test5.cpp(158) : error C2589: '(' : illegal token on right side of '::'
    test5.cpp(158) : error C3861: 'numeric_limits': identifier not found, even with argument-dependent lookup

    It's like the compiler isn't recognizing "#include <limits>" when "#include <windows.h>" is inserted. I even tried moving "#include <limits>" around, but to no avail. When i take out "#include <windows.h>" everything compiles with NO errors. I don't get it.
     
  11. Oct 30, 2007 #10

    KTC

    User Avatar

    Oh the joy of <windows.h>, or rather name crashes. (This is why there's namespace in C++.)

    It doesn't compile because max is defined as a macro in <windows.h> which expand when the compiler* see max(*, *).

    You have two options:
    1. Tell <windows.h> not to define the macro by defining NOMINMAX before including <windows.h>.
    Code (Text):
    #define NOMINMAX

    #include <windows.h>

    #include <iostream>
    .......
    Open and read windows.h for a complete list of such flags.

    2. Enclose your *::max in () to cause the macro not to be expanded.
    Code (Text):
    cin.ignore((numeric_limits<int>::max)(), '\n');

    ---
    * Preprocessor
     
  12. Oct 30, 2007 #11
    Well, inserting "#define NOMINMAX" before "include <windows.h> cut the errors down to 4:
    test5.cpp(153) : error C2065: 'numeric_limits' : undeclared identifier
    test5.cpp(153) : error C2062: type 'int' unexpected
    test5.cpp(162) : error C2062: type 'int' unexpected
    test5.cpp(162) : error C3861: 'numeric_limits': identifier not found, even with argument-dependent lookup

    Then I tried the "cin.ignore((numeric_limits<int>::max)(), '\n');" statement without the "#define NOMINMAX", but it gave me more errors and warnings. Then I tried them both, but NO GO!
     
  13. Oct 30, 2007 #12

    KTC

    User Avatar

    In that case, you'll need to provide more of your code, as the following compile fine for me.

    Code (Text):
    #define NOMINMAX

    #include <windows.h>

    #include <iostream>
    #include <limits>

    using namespace std;

    bool wrong = false;

    bool numbercheck(int& integer)
    {
        if (cin)
        {
            cin.ignore(numeric_limits<int>::max(), '\n');
            if (cin.gcount() == 1)
            wrong = true;
            //cout << "YOU GOT IT RIGHT!" << endl;
            return true;
        }
        else
        {
            cin.clear();
            cin.ignore(numeric_limits<int>::max(), '\n');
            cout << "\n\nWRONG CHARACTER! TRY AGAIN......" << endl;
            cout << endl;
            return false;
        }
    }

    int main()
    {
    }
     
     
  14. Oct 30, 2007 #13

    KTC

    User Avatar

    Hmm, (numeric_limits<int>::max)() doesn't work, although in theory it should (I think anyway).

    Anyway, #define NOMINMAX should work.
     
  15. Oct 30, 2007 #14
    Ok! I'll Globalize the numbercheck_Function and see if it works with the entire program. I just tried it by itself Globalized and it compiled with NO errors. Thanx once again all of your help! I'll post the results in a few.
     
  16. Oct 30, 2007 #15
    Ok! Why making the numbercheck_Function Global make this work? Could anybody explain this "PHENOMENON?"

    @KTC: I really appreciate your help on this. I kinda fill bad for you, because I know who to go to for answers. You know what they say, "Don't feed the bears!" Well I just ate good. LOL!! Take care!
     
  17. Oct 31, 2007 #16

    KTC

    User Avatar

    Again, I have no problem. i.e. I can't reproduce your error. It would help if you can provide a minimum example which reproduce the problem you're having, and any error messages you're getting.

    Code (Text):
    #define NOMINMAX

    #include <windows.h>

    #include <iostream>
    #include <limits>

    using namespace std;

    class TestClass
    {
    public:
        TestClass();

        bool numbercheck(int& integer);
    private:
        bool wrong;
    };

    TestClass::TestClass()
        : wrong(false)
    {}

    bool TestClass::numbercheck(int& integer)
    {
        if (cin)
        {
            cin.ignore(numeric_limits<int>::max(), '\n');
            if (cin.gcount() == 1)
            wrong = true;
            //cout << "YOU GOT IT RIGHT!" << endl;
            return true;
        }
        else
        {
            cin.clear();
            cin.ignore(numeric_limits<int>::max(), '\n');
            cout << "\n\nWRONG CHARACTER! TRY AGAIN......" << endl;
            cout << endl;
            return false;
        }
    }

    int main()
    {
        TestClass tc;

        return 0;
    }
     
    The above work for me.
     
  18. Nov 1, 2007 #17
    I guess I wasn't clear enough in my previous post. Sorry! Everything works now. Globalizing the numbercheck_Function made the program work for some strange reason. Thanx Again!!!
     
  19. Nov 1, 2007 #18

    KTC

    User Avatar

    By globalizing, I take it you mean you had it as a member function of a class. Again, my last post shows everything working even when it is a member function, so I don't know what problem you had.
     
  20. Nov 1, 2007 #19
    This program is straight Procedural Style Programming. There are no classes involved. What I mean by "Globalizing," which I doubt is the correct term, is basically inserting the numbercheck_Function before the main_Function.LOL! That's all. Sorry for the confusion.
     
  21. Nov 1, 2007 #20

    rcgldr

    User Avatar
    Homework Helper

    I find that I have to have a "using namespace std" inside of every function that calls one of the "std class" functions, such as cout, for example:

    int main(int argc, char **argv)
    {
    using namespace std;
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?



Similar Discussions: Include header files order?
Loading...