CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Jun 2010

    variable undeclared (first use in this funcion)

    #include <iostream>
    #include <fstream>
    using namespace std;
    int prime [10000];
    int index = 0, min, max;
    bool isPrime(int x);
    void genPal(int num, int add, int mulleft, int mulright);
    int main(){
    	ifstream fin ("pprime.in");
    	ofstream fout ("pprime.out");
    	fin >> min >> max;
    	int max2 = max;
    	int length;
    	for(length = 0; max2; length++){
    		max2 /= 10;
    	cout << length << '\n';
    bool isPrime(int x){
    	if(x < min || x > max || x%2 == 0 || x%3 == 0)
    		return false; 
    	for(int i = 2; i*i < x; i++)
    		if(x%i == 0)
    			return 0;
    	return true;
    void genPal(int num, int add, int mulleft, int mulright){
    	int i, nmulleft, nmulright;
    	if(num == 2)
    		for(i = 0; i < 10; i++)
    			 add+mulleft * i + mulright * i << '\n'; 
    	else if(num == 1)
    		for(i = 0; i < 10; i++)
    			cout << add + mulright * i << '\n';
    		nmulleft = mulleft / 10;
    		nmulright = mulright * 10;
    		num -= 2;
    		for(i = 0; i < 10; i++)
    			genPal(num, add+i*mulleft + i*mulright, nmulleft, nmulright);
    The errors:
    test.cpp: In function 'int main()':
    test.cpp:15: error 'min' undeclared(first use in this function)
    and others like this. How do I fix this without making the variable min local to the main method?

  2. #2
    Join Date
    Jan 2006

    Re: variable undeclared (first use in this funcion)

    It is a bad idea to use variables named min and max, especially with the using directive using namespace std, due to possible conflicts with std::min and std::max (and then possible conflicts with certain Windows specific macros named min and max).

    Incidentally, you should avoid the use of global variables, though in this case a global array of primes is arguably tolerable. The min and max global variables, on the other hand, are not so tolerable, and index is not even used.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

Tags for this Thread

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)

By using this site, you agree to the Privacy Policy