-
June 1st, 2010, 01:31 PM
#1
variable undeclared (first use in this funcion)
Code:
#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';
else{
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?
-
June 1st, 2010, 01:37 PM
#2
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.
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|