error: called object type 'int' is not a function or function pointer
Solution 1
Your local variable mid
is declared in the scope that is closer to the point of use, so it "shadows" the mid()
function; the compiler thinks that you are trying to "call" an integer, which is invalid. Rename the local variable to fix this problem:
int midpoint;
if (hi > lo) {
int i = lo + 1;
int j = hi;
int p = mid(lo, hi);
swap(vec[lo], vec[p]);
midpoint = vec[lo];
...
}
Note: you could also use ::mid(lo, hi)
instead of renaming the variable, but that would confuse the readers of your program.
Solution 2
int mid(int lo, int hi); // here you declared mid as function and defined
// it later
// My quicksort implementation
void sort(int vec[], int lo, int hi)
{
int mid; // but here you declared mid as local variable
if (hi > lo) { // it will shadow int mid(int lo, int hi);
int i = lo + 1;
int j = hi;
int p = mid(lo, hi); // so this is error, mid is integer not a function
you can change the name of variable in the algorithm or use scope resolution operator ::mid(lo, hi)
to access mid
function previously defined in global scope
Related videos on Youtube

Pocketkid2
I love programming, but I do it more as a hobby, as I'm still in high school and have a pretty busy schedule. That explains most of my nooby questions and behaviors. I am pretty good at Java, and I much enjoy it. I'm OK at C, and C++. I am trying to learn Swift. I used to know HTML and CSS. I hate python and JavaScript. That's about all the languages I've really come across
Updated on July 09, 2022Comments
-
Pocketkid2 11 months
I have a quicksort that I wrote here:
void swap(int& a, int& b); int mid(int lo, int hi); // My quicksort implementation void sort(int vec[], int lo, int hi) { int mid; if (hi > lo) { int i = lo + 1; int j = hi; int p = mid(lo, hi); swap(vec[lo], vec[p]); mid = vec[lo]; while (i < j) { if (vec[i] <= mid) { i++; } else { while (i < --j && vec[j] >= mid); swap(vec[i], vec[j]); } } i++; swap(vec[lo], vec[i]); sort(vec, lo, i); sort(vec, j, hi); } } void swap(int& a, int& b) { int temp = a; a = b; b = temp; } int mid(int lo, int hi) { return lo + ((hi - lo) / 2); }
I tried compiling to an object file with
g++ -g -c array.cpp -o array.o
I get this error:array.cpp:24:14: error: called object type 'int' is not a function or function pointer int p = mid(lo, hi); ~~~^ 1 error generated.
Everything looks correct. Can anyone help me figure out what is wrong?
-
chris over 9 yearsYou declared
int mid;
but callmid(lo, hi)
...
-