I'm trying to do my homework, but I can't understand the errors that I'm getting. By looking at the examples in my book, everything is perfect, but it doesn't want to compile. Can anyone tell me what's wrong?
In function `int range(int, int)':
34 assignment of function `int range(int, int)'
34 cannot convert `int' to `int ()(int, int)' in assignment
35 invalid conversion from `int (*)(int, int)' to `int'
In function `int main()':
83 invalid conversion from `int (*)()' to `int'
83 initializing argument 1 of `int range(int, int)'
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <string>
#include <cmath>
usingnamespace std;
int n;
int numbers[99];
int minimum()
{
int minimum;
minimum = numbers[0];
return minimum;
}
int maximum()
{
int maximum;
maximum = numbers[98];
return maximum;
}
int median()
{
int median = numbers[47];
return median;
}
int range(int maxx,int minn)
{
range = maxx - minn;
return range;
}
float mean()
{
int mean;
int c;
int a;
for (a = 1;a <= 98;a++)
{
c = a - 1;
mean = numbers[a] + numbers[c];
}
mean /= 98;
return mean;
}
int main()
{
int i;
ifstream fin;
fin.open("numbers.txt");
if (!fin.good()) throw"I/O error";
fin >> n;
fin >> numbers[98];
fin.ignore(1000, 10);
for (i = 0; i < n; i++)
{
int j;
for (j =i + 1; j < n; j++)
{
if (numbers[i] > numbers[j])
{
int temp = numbers[i];
numbers[i] = numbers[j];
numbers[j] = temp;
}
}
}
fin.close();
minimum();
maximum();
median();
range(maximum, minimum);
mean();
system("PAUSE");
return 0;
}
LOL newbies. You still haven't seen template compiler errors or linker errors. You can only use your intuition to fix those, because reading them won't help much. Some of them can easily reach 1000 characters in length.
Wow I feel like an idiot now, I always have some stupid little syntax error. Thanks
Now I'm getting
In function `int main()':
83 invalid conversion from `int (*)()' to `int'
83 initializing argument 1 of `int range(int, int)'
In function `int main()':
83 invalid conversion from `int (*)()' to `int'
83 initializing argument 2 of `int range(int, int)'
What does it mean, by conversion?
P.S. Yeah, this book sucks, but it was written by the head of the department so we have to use it. It teaches double in the fist 20 pages, but doesn't teach float until after functions.
maximum and minimum (without the parentheses) are pointers to maximum() and minimum(). If you want a function to be called, you have to put the parentheses, otherwise, you're just getting the pointer: range(maximum(), minimum());
I think that is an old standard, though. IINM, in the new standard referring to a function without its parentheses just evaluates to true, rather than getting the function pointer.
Hey, that worked helios. I thought that I could just use the returned value from maximum and minimum rather than running the function again. Oh well, thanks for all your help.