【问题标题】:expression list treated as compound expression in functional cast [-fpermissive]表达式列表在功能转换中被视为复合表达式 [-fpermissive]
【发布时间】:2014-03-12 02:53:42
【问题描述】:
#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: 在函数'void SelectionSort(int*, int, compare)'中: test.cpp:14:40:错误:表达式列表被视为功能转换中的复合表达式 [-fpermissive] test.cpp:14:40:警告:从不同大小的整数转换为指针 [-Wint-to-pointer-cast]

为什么会出现错误。我来自 C 背景。以上我认为在“C”中完全有效 为什么在 C++ 中会发生这种情况?

【问题讨论】:

  • 这在 C 中不再有效(事实上,不太有效,因为函数转换不是一件事)。 compare 是一个类型,而不是一个函数。

标签: c++


【解决方案1】:

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]))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-05
    • 1970-01-01
    • 2012-01-15
    相关资源
    最近更新 更多