【问题标题】:function-definition is not allowed here before ‘{’ token"在‘{’标记之前不允许函数定义”
【发布时间】:2019-03-10 10:37:34
【问题描述】:

我在我的 qt 项目 m 中包含了“Edge_collapse_recorder.h”,我想在折叠过程中打印一些值,但是当我取消注释 std::cout 行:

../CM2/edge_collapse_recorder.h:102:37: error: no match for ‘operator<<’ (operand types are ‘std::basic_ostream<char>’ and ‘const halfedge_descriptor’ {aka ‘const CGAL::internal::CC_iterator<CGAL::Compact_container<CGAL::Dart<2, CGAL::CMap_linear_cell_complex_storage_1<2, 3, CGAL::Linear_cell_complex_traits<3, CGAL::Simple_cartesian<double> >, CGAL::Linear_cell_complex_bgl_min_items, std::allocator<int> >, CGAL::Void, CGAL::Boolean_tag<true> >, std::allocator<CGAL::Dart<2, CGAL::CMap_linear_cell_complex_storage_1<2, 3, CGAL::Linear_cell_complex_traits<3, CGAL::Simple_cartesian<double> >, CGAL::Linear_cell_complex_bgl_min_items, std::allocator<int> >, CGAL::Void, CGAL::Boolean_tag<true> > >, CGAL::Default, CGAL::Default>, false>’})
       std::cout << "collapse edge " << profile.v0_v1() << " " << << profile.v0()  << " " << profile.v1() << std::endl;
       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~

我搜索了一下问题,发现必须重载

 std::ostream& operator<<(std::ostream& os, const Edge_collapse_recorder& t) {
                      os <<  << "collapse edge " << t.v0_v1() << " " << << t.v0()  << " " << t.v1();
                     return os;
                 }

但它显示以下错误: 此处不允许函数定义在‘{’令牌之前”

  void OnCollapsing(Profile const& profile
                      ,boost::optional<Point_3>  placement
                      )
    {
      visitor.OnCollapsing(profile,placement);

      if ( placement ){
           std::ostream& operator<<(std::ostream& os, const Edge_collapse_recorder& t) {
                     os <<  << "collapse edge " << t.v0_v1() << " " << << t.v0()  << " " << t.v1();
                     return os;
                 }

        Record record;
        record.v0 = profile.v0();
        record.v1 = profile.v1();
        record.p0 = profile.p0();
        record.p1 = profile.p1();
        halfedge_descriptor hd = profile.v0_v1();

        record.oppa = (! is_border(hd,recorder.sm))
          ? target(next(hd, recorder.sm),recorder.sm)
          : boost::graph_traits<LCC>::null_vertex();

        record.oppb = (! is_border(opposite(hd, recorder.sm),recorder.sm))
          ? target(next(opposite(hd, recorder.sm), recorder.sm),recorder.sm)
          : boost::graph_traits<LCC>::null_vertex();

        recorder.records.push_back(record);
      }
    }

【问题讨论】:

  • 首先,不要将代码发布为图片。代码是文本,复制粘贴。否则就是阻碍搜索能力并使视障者的事情变得更加困难。其次,图像包含几个语义和句法问题,表明您可能需要good book about C++
  • 函数不能在其他函数的主体中定义。但这正是违规行试图做的——在OnCollapsing() 的主体中定义而不是调用名为operator&lt;&lt;() 的函数。
  • @Peter 好的,我明白你的意思,但是我如何使用 cout
  • cout &lt;&lt; value_you_want_output。是什么让你觉得在OnCollapsing() 中使用“cout OnCollapsing() 添加了一些文本 - 为什么使用该特定文本?
  • @Peter 你是对的,我现在尝试了 cout' 和 'const halfedge_descriptor' 我更新了打印 profile.v0() 时显示错误的帖子

标签: c++ qt cgal


【解决方案1】:

被注释掉的那一行永远不会编译。它有两个连续的&lt;&lt; 运算符。

至于报错信息,你是在定义函数

std::ostream& operator<<(std::ostream& os, const Edge_collapse_recorder& t)

(而不仅仅是声明它)在另一个函数中。 C++ 中不允许嵌套函数定义。做你想做的事(我不确定这是否是个好主意),把函数定义放在OnCollapsing函数之前之前

【讨论】:

  • 鉴于OnCollapsing() 函数定义但从未调用operator&lt;&lt;() 函数,因此无需在OnCollapsing() 之前定义operator&lt;&lt;()。唯一的限制是定义不能嵌套。可能需要有一个可见的声明,但定义不需要。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-08-31
  • 1970-01-01
  • 2023-02-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多