Is there an issue with nested functions and using namespaces?

  • Thread starter sweetjones
  • Start date
  • Tags
    files
In summary, the professor gave me a template for commonly used headers, but I don't have a "using namespace std" in my code. When I try to include a header using the "include" directive, MSVC++ starts to process the directive but then throws an error. I need to insert the "windows.h" somewhere in my code, but I'm not sure
  • #1
sweetjones
44
0
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!
 
Technology news on Phys.org
  • #2
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>.
 
  • #3
KTC said:
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>.
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:
  • #4
Do you have a "using namespace std" in your code?

If not, then the syntax is "std::cin", "std::cout", ...
 
  • #5
sweetjones said:
#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>

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:
#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.
 
  • #6
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.
 
  • #7
Jeff Reid said:
Do you have a "using namespace std" in your code?

If not, then the syntax is "std::cin", "std::cout", ...

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"?
 
  • #8
jtbell said:
And "iostream" should have angle brackets instead.
Dr Transport said:
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.

No, "header" works as well.
C++ Standard 2003 16.2.3 said:
A preprocessing directive of the form
Code:
# include "q-char-sequence" new-line
causes the replacement of that directive by the entire contents of the source file identified by the specified sequence between the " delimiters. The named source file is searched for in an implementation-defined manner. If this search is not supported, or if the search fails, the directive is reprocessed as if it read
Code:
# include <h-char-sequence> new-line
with the identical contained sequence (including > characters, if any) from the original directive.



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

Where should insert the "windows.h"?
Before every other header file inclusion, except that of <stdafx.h> if you're using precompiled header on MSVC++.

Code:
#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.
 
  • #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.
 
  • #10
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:
#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:
cin.ignore((numeric_limits<int>::max)(), '\n');
---
* Preprocessor
 
  • #11
KTC said:
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:
#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:
cin.ignore((numeric_limits<int>::max)(), '\n');


---
* Preprocessor

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!
 
  • #12
In that case, you'll need to provide more of your code, as the following compile fine for me.

Code:
#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()
{
}
 
  • #13
Hmm, (numeric_limits<int>::max)() doesn't work, although in theory it should (I think anyway).

Anyway, #define NOMINMAX should work.
 
  • #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.
 
  • #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!
 
  • #16
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:
#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.
 
  • #17
KTC said:
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.


The above work for me.

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!
 
  • #18
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.
 
  • #19
KTC said:
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.

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.
 
  • #20
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;
 
  • #21
sweetjones said:
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.
Well yeah, one can't nest functions in C++.


Jeff Reid said:
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:
I'm not sure whether that is a question or not. :confused:
C++ Standard said:
A using-directive specifies that the names in the nominated namespace can be used in the scope in which the using-directive appears after the using-directive. During unqualified name lookup (3.4.1), the names appear as if they were declared in the nearest enclosing namespace which contains both the using-directive and the nominated namespace. [Note: in this context, “contains” means “contains directly or indirectly”. ]
 

FAQ: Is there an issue with nested functions and using namespaces?

What is the purpose of including header files in a specific order?

The order in which header files are included in a program is important because it determines the order in which the compiler will read and process the declarations and definitions contained within the header files. This can affect the behavior and functionality of the program.

What happens if header files are included in the wrong order?

If header files are included in the wrong order, it can lead to compilation errors or unexpected behavior in the program. This is because the compiler may not be able to find the necessary declarations or definitions, or may encounter conflicting declarations.

Is there a specific order in which header files should be included?

Yes, there is a recommended order for including header files. Generally, system headers should be included first, followed by external libraries, and finally any local project-specific header files. This helps to prevent conflicts and ensure that all necessary declarations are available.

Can header files be included multiple times in a program?

Yes, header files can be included multiple times in a program. However, using include guards or the #pragma once directive can help to prevent multiple inclusions and improve compilation performance.

Are there any exceptions to the recommended order of including header files?

In some cases, the order of including header files may not matter. This is typically true for header files that only contain function prototypes and do not have any dependencies on other header files. However, it is generally considered good practice to follow the recommended order to avoid potential issues in the future.

Back
Top