C++ C++;-函数指针和类

C++ C++;-函数指针和类,c++,function,class,pointers,C++,Function,Class,Pointers,当我试图编译这段代码以使用C++中的类实现函数指针的概念时: #include <iostream> using namespace std; class sorting { public: void bubble_sort(int *arr, int size, bool (*compare)(int,int)) { int i,j,temp = 0; for(i = 0; i < size - 1; i++) { for(j =

当我试图编译这段代码以使用C++中的类实现函数指针的概念时:

#include <iostream>

using namespace std;

class sorting
{
public:
void bubble_sort(int *arr, int size, bool (*compare)(int,int))
{
    int i,j,temp = 0;

    for(i = 0; i < size - 1; i++)
    {
        for(j = i+1; j < size; j++)
        {
            if(compare(arr[i],arr[j]))
            {
                temp = arr[i];
                arr[i] = arr[j];
                arr[j]  = temp;
            }
        }
    }
}

bool ascending(int x,int y)
{
    return x > y;
}

bool descending(int x,int y)
{
    return x < y;
}

void display(int *arr,int size)
{
    for(int index = 0; index < size; index++)
    {
        cout<<"arr["<<index<<"]:"<<arr[index]<<endl;
    }
}
};

int main()
{
    int arr[10] = {99,77,22,33,88,55,44,66,11,100};

    sorting s;
    cout<<"Ascending order"<<endl;
    s.bubble_sort(arr,10,&sorting::ascending);
    s.display(arr,10);

    cout<<"Descending order"<<endl;
    s.bubble_sort(arr,10,&sorting::descending);
    s.display(arr,10);

    return 0;
}
错误是:

error C2664: 'sorting::bubble_sort' : cannot convert parameter 3 from 'bool (__thiscall sorting::* )(int,int)' to 'bool (__cdecl *)(int,int)'
两条线都有。
有人能帮我消除这些错误吗?

排序::升序
排序::降序
应该声明为
静态
,或者根本不作为成员,因为它们不操作
*此
实例。这就是您如何知道函数应该是静态的(或非成员的)


如果不将它们声明为
静态
,则成员函数指针的语法是不同的,您还需要一个伪实例来进行调用。

升序
降序
是成员函数,因此它们只能在
排序
类成员上调用(实际上有三个论点,而不是两个)


使它们成为
静态
函数,或者更好的是,将
排序
更改为
命名空间
:它没有理由成为类。

最简单的方法是在类定义内部或外部定义
升序
降序
静态函数,或将函数声明/定义移到类的作用域之外:

static bool ascending(int x,int y)
{
    return x > y;
}

static bool descending(int x,int y)
{
    return x < y;
}
静态布尔升序(整数x,整数y)
{
返回x>y;
}
静态布尔递减(整数x,整数y)
{
返回x

<>编译器使用类代码定义的类函数作为成员函数,使用“代码”>“调用/<代码>调用约定”,而普通C++函数指针则默认使用<代码>将类的定义移动到C++类之外,将函数定义为<代码>·y/cDell < /C>修饰符。将函数标记为<代码> static cDell < /C>修饰符。

函数指针在C++中略有不同。不同于C,成员函数不总是“全局”可访问的,除非生成<代码>static
。当设置为static时,对于该类的任意数量的实例,函数只有一个地址位置。使用此方法的代码将是:

#include<iostream>

using namespace std;

class sorting
{
public:
void bubble_sort(int *arr, int size, bool (*compare)(int,int))
{
    int i,j,temp = 0;

    for(i = 0; i < size - 1; i++)
    {
        for(j = i+1; j < size; j++)
        {
            if(compare(arr[i],arr[j]))
            {
                temp = arr[i];
                arr[i] = arr[j];
                arr[j]  = temp;
            }
        }
    }
}

static bool ascending(int x,int y)
{
    return x > y;
}

static bool descending(int x,int y)
{
    return x < y;
}

void display(int *arr,int size)
{
    for(int index = 0; index < size; index++)
    {
        cout<<"arr["<<index<<"]:"<<arr[index]<<endl;
    }
}
};

int main()
{
    int arr[10] = {99,77,22,33,88,55,44,66,11,100};

    sorting s;
    cout<<"Ascending order"<<endl;
    s.bubble_sort(arr,10,&sorting::ascending);
    s.display(arr,10);

    cout<<"Descending order"<<endl;
    s.bubble_sort(arr,10,&sorting::descending);
    s.display(arr,10);

    return 0;
}

最简单的解决方案?使比较函数成为静态的。然后跟着我重复:指向函数的指针与指向成员函数的指针是不同的。最简单的解决方法是将升序的
和降序的
标记为静态的
#include<iostream>

using namespace std;

class sorting
{
public:
void bubble_sort(int *arr, int size, bool (*compare)(int,int))
{
    int i,j,temp = 0;

    for(i = 0; i < size - 1; i++)
    {
        for(j = i+1; j < size; j++)
        {
            if(compare(arr[i],arr[j]))
            {
                temp = arr[i];
                arr[i] = arr[j];
                arr[j]  = temp;
            }
        }
    }
}

static bool ascending(int x,int y)
{
    return x > y;
}

static bool descending(int x,int y)
{
    return x < y;
}

void display(int *arr,int size)
{
    for(int index = 0; index < size; index++)
    {
        cout<<"arr["<<index<<"]:"<<arr[index]<<endl;
    }
}
};

int main()
{
    int arr[10] = {99,77,22,33,88,55,44,66,11,100};

    sorting s;
    cout<<"Ascending order"<<endl;
    s.bubble_sort(arr,10,&sorting::ascending);
    s.display(arr,10);

    cout<<"Descending order"<<endl;
    s.bubble_sort(arr,10,&sorting::descending);
    s.display(arr,10);

    return 0;
}
#include<iostream>

using namespace std;

class sorting
{
public:
void bubble_sort(int *arr, int size, bool (sorting::*compare)(int,int))
{
    int i,j,temp = 0;

    for(i = 0; i < size - 1; i++)
    {
        for(j = i+1; j < size; j++)
        {
            if((this->*compare)(arr[i],arr[j]))
            {
                temp = arr[i];
                arr[i] = arr[j];
                arr[j]  = temp;
            }
        }
    }
}

bool ascending(int x,int y)
{
    return x > y;
}

bool descending(int x,int y)
{
    return x < y;
}

void display(int *arr,int size)
{
    for(int index = 0; index < size; index++)
    {
        cout<<"arr["<<index<<"]:"<<arr[index]<<endl;
    }
}
};

int main()
{
    int arr[10] = {99,77,22,33,88,55,44,66,11,100};

    sorting s;
    cout<<"Ascending order"<<endl;
    s.bubble_sort(arr,10,&sorting::ascending);
    s.display(arr,10);

    cout<<"Descending order"<<endl;
    s.bubble_sort(arr,10,&sorting::descending);
    s.display(arr,10);

    return 0;
}