表达式列表在功能转换中被视为复合表达式[-fpermissive]

时间:2014-03-12 02:53:43

标签: c++

#include <iostream>
#include <algorithm>

using namespace std;

typedef bool (*compare)(int,int);

void SelectionSort(int *inArray,int size, compare)
{
        for (int loop = 0 ; loop < size ; loop++)
        {
                for(int j = loop+1 ; j < size ; j++)
                {
                        if (compare(inArray[j],inArray[loop]))
                                swap(inArray[loop],inArray[j]);
                }
        }
}

void display(int *inArray,int size)
{
        cout << "Printing the array " << "\n" << endl;
        for(int loop = 0; loop < size; loop++)
        {
                cout << inArray[loop] << endl;
        }

}

bool ascending(int a , int b)
{
        if(a < b)
                return true;
        else
                return false;
}

bool descending(int a,int b)
{
        if (a > b)
                return true;
        else
                return false;
}


int main()
{

        compare c1 = ascending;
        compare c2 = descending;
        int pList[5] = {50,40,30,20,10};

        display(pList,5);
        SelectionSort(pList,5,c1);
        display(pList,5);
        SelectionSort(pList,5,c2);
        display(pList,5);

}

$ g ++ test.cpp test.cpp:在函数&#39; void SelectionSort(int *,int,compare)&#39;: test.cpp:14:40:error:表达式列表在功能转换中被视为复合表达式[-fpermissive] test.cpp:14:40:警告:从不同大小的整数转换为指针[-Wint-to-pointer-cast]

为什么出现错误。我来自C背景。上面我认为在C&#39; C&#39; 为什么会在C ++中发生这种情况?

1 个答案:

答案 0 :(得分:2)

typedef并不符合您的意思。在C ++中typedef创建一个类型别名。例如:

typedef int INT;
INT i = 5; //i is of type int

所以你的代码应该是:

void SelectionSort(int *inArray,int size, compare comp)
...
if (comp(inArray[j],inArray[loop]))