C++ 错误:被调用的对象类型“int”不是函数或函数指针

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19733090/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 23:05:47  来源:igfitidea点击:

error: called object type 'int' is not a function or function pointer

c++quicksort

提问by Pocketkid2

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.oI get this error:

我尝试编译为目标文件,g++ -g -c array.cpp -o array.o但出现此错误:

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?

一切看起来都正确。谁能帮我弄清楚出了什么问题?

回答by dasblinkenlight

Your local variable midis 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:

您的局部变量mid是在更接近使用点的范围内声明的,因此它“遮蔽”了mid()函数;编译器认为您正在尝试“调用”一个无效的整数。重命名局部变量以解决此问题:

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.

注意:您也可以使用::mid(lo, hi)而不是重命名变量,但这会使程序的读者感到困惑。

回答by 4pie0

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 midfunction previously defined in global scope

您可以更改算法中的变量名称或使用范围解析运算符::mid(lo, hi)来访问mid先前在全局范围中定义的函数