c++ - 此处不允许在 ‘{’ token 之前定义函数”

标签 c++ qt cgal

我在我的 qt 项目 m 中包含了“Edge_collapse_recorder.h”,我想在折叠过程中打印一些值,但是当我取消注释 std::cout << "collapse edge "<< profile 时它显示以下错误.v0_v1() << ""<<<< profile.v0() << ""<< profile.v1() << std::endl; 行:

../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;
       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~

我搜索了这个问题,发现我必须重载 << 运算符,然后我将以下内容添加到 onCollapssing 方法:

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

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

  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);
      }
    }

最佳答案

那条被注释掉的行永远不会编译。它有两个连续的 <<运营商。

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

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

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

关于c++ - 此处不允许在 ‘{’ token 之前定义函数”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55086798/

相关文章:

C++ QPushButton 信号和槽

c++ - QT Creator 中没有警告

c++ - 如何在感兴趣的区域细化cgal中的网格

c++ - CGAL 遍历共享边的面

C++14 类型列表,有什么理由更喜欢 'free functions' 到 'methods',反之亦然?

c++ - gmock : check only one element of vector

c++ - 在 QApplication 中使用可视化检漏仪

C++ 分配的指针显示意外行为

c++ - 为什么在将函数分配给函数指针时可以多次使用解引用运算符?

c++ - 将 Eigen::MatrixXd 矩阵转换为 cgal 容器类型