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

Expression list treated as compound expression

我正在尝试编译从网上获得的程序。尝试在代码块中使用但显示错误。我不明白出了什么问题。我在各种论坛上查找过,但没有太多的启发。有人可以帮忙吗?提前致谢

#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 call\n";
    }

    return 0;
}

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

main.cpp|10|error: 'function' is not a member of 'std'
main.cpp|10|error: expression list treated as compound expression in functional cast [-fpermissive]
main.cpp|10|error: expected primary-expression before '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’
...