表达式列表被视为复合表达式

Expression list treated as compound expression

本文关键字:表达式 复合 列表      更新时间:2023-10-16

我正在尝试编译一个从网上得到的程序。尝试在代码块中使用,但它显示错误。我不明白出了什么问题。我在各种论坛上查找过,但没有太多的线索。谁能帮忙很快?提前致谢

#include <functional>
#include <iostream>
int print_num(int i, int j) { return i + j; }
int main() {
    std::function<int(int, int)> foo = print_num;
    std::function<int(int, int)> bar;
    try {
        std::cout << foo(10, 20) << 'n';
        std::cout << bar(10, 20) << 'n';
    } catch (std::bad_function_call& e) {
        std::cout << "ERROR: Bad function calln";
    }
    return 0;
}

这些是除 14 个其他错误之外的一些错误,表示声明未完成。我想清除这些错误可以解决这个问题.

main.cpp|10|error:"function"不是"std"
的成员 main.cpp|10|错误:表达式列表在函数强制转换中被视为复合表达式 [-允许]
main.cpp|10|error:在"int"之前预期的主表达式

您需要

使用-std=c++11进行编译才能添加C++11功能。

$ g++ -std=c++11 test.cxx && ./a.out
30
ERROR: Bad function call

与:

$ g++ test.cxx && ./a.out
test.cxx: In function ‘int main()’:
test.cxx:10:3: error: ‘function’ is not a member of ‘std’
test.cxx:10:28: error: expression list treated as compound expression in functional cast [-fpermissive]
test.cxx:10:17: error: expected primary-expression before ‘int’
...