【问题标题】:Expression list treated as compound expression表达式列表被视为复合表达式
【发布时间】:2015-04-10 19:49:47
【问题描述】:

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

#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|错误:'function'不是'std'的成员
main.cpp|10|错误:表达式列表被视为函数转换中的复合表达式 [-fpermissive]
main.cpp|10|error: 在 'int' 之前的预期主表达式

【问题讨论】:

  • 你是用 C++11 编译的吗?
  • 始终包含顶部的错误。早期错误经常描述后来错误发生的原因。包括您的编译器是什么,以及您使用的编译器标志。

标签: c++ exception-handling


【解决方案1】:

您需要使用-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’
...

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-27
    • 2017-12-31
    相关资源
    最近更新 更多