表达列表作为复合表达处理

时间:2015-04-10 19:49:47

标签: c++ exception-handling

我正在尝试编译我从网上获得的程序。试图在代码块中使用但显示错误。我不明白出了什么问题。我在各种论坛上都抬起头来,但没有多少光明。有人可以帮忙吗?提前致谢

#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 |错误:&#39;功能&#39;不是&#39;标准的成员   main.cpp | 10 | error:表达式列表在功能转换中被视为复合表达式[-fpermissive]
  main.cpp | 10 |错误:预期的主要表达式&#39; int&#39;

1 个答案:

答案 0 :(得分:1)

您需要使用-std=c++11进行编译以添加C ++ 11功能。

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

VS

$ 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’
...
相关问题