【问题标题】:How can I know what makes an incomplete type incomplete for error: arithmetic on a pointer to an incomplete type我怎么知道是什么导致不完整类型因错误而不完整:对指向不完整类型的指针的算术运算
【发布时间】:2016-09-03 18:15:35
【问题描述】:

我试图定义我自己的 PointT 类型,以便在一个点上给我 XYZRGBA 和强度值。我尝试在点云库网站上遵循本文中列出的约定:http://pointclouds.org/documentation/tutorials/adding_custom_ptype.php

PointXYZRGBAI 的实现在我的一个类的 .CPP 文件中,但我在其他头文件包含的头文件中转发声明它。实现在 LidarFile.cpp 中:

struct PointXYZRGBAI{
  PCL_ADD_POINT4D;
  union{
    struct{
      float intensity;
      uint32_t rgba;
    };
    float data_c[4];
  };
  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
} EIGEN_ALIGN_16;

POINT_CLOUD_REGISTER_POINT_STRUCT(PointXYZRGBAI,
                                  (float, x, x)
                                  (float, y, y)
                                  (float, z, z)
                                  (float, intensity, intensity)
                                  (uint32_t, rgba, rgba)
)

inline std::ostream& operator << (std::ostream& os, const PointXYZRGBAI& p){
  os << "(" << p.x << ", " << p.y << ", " << p.z << " - " << p.intensity << " - " << p.rgba << ")";
  return (os);
}

前向声明位于头文件名 PointXYZRGBAI.h 中

#define PCL_NO_PRECOMPILE

#ifndef POINTXYZRGBAI_H
#define POINTXYZRGBAI_H
#endif

#include <pcl/point_types.h>

struct PointXYZRGBAI;

inline std::ostream& operator << (std::ostream& os, const PointXYZRGBAI& p);

从我对结构体的基本理解来看,我需要转发声明结构体,以便编译器在编译过程中理解PointXYZRGBAI确实是一个结构体而不是某种未知类型,然后用实际实现来填补空白。但这似乎受到以下事实的挑战:当我声明任何使用模板类型 PointXYZRGBAI 的成员实例变量时,会引发错误:对指向不完整类型 'PointXYZRGBAI 的指针的算术运算,就像我声明使用该结构的点云时一样点XYZRGBAI:

pcl::PointCloud<PointXYZRGBAI> cl; //Error: arithmetic on a pointer to an incomplete type 'PointXYZRGBAI'
//__alloc_traits::destroy(__alloc(), _VSTD::__to_raw_pointer(--__end_));
// __alloc_traits::destroy(__alloc(), _VSTD::__to_raw_pointer(--__end_));

似乎在 PointXYZRGBAI 类型上发生了未定义的操作。所以我该怎么做?我是否需要在 LidarFile.h 标头之前以某种方式实现该结构?仅供参考,这里是完整的错误堆栈跟踪:

In file included from /Users/wfehrnstrom/Demeter/core.cpp:9:
In file included from /usr/local/include/boost/thread/thread.hpp:12:
In file included from /usr/local/include/boost/thread/thread_only.hpp:17:
In file included from /usr/local/include/boost/thread/pthread/thread_data.hpp:24:
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/vector:424:68: error: 
      arithmetic on a pointer to an incomplete type 'PointXYZRGBAI'
        __alloc_traits::destroy(__alloc(), _VSTD::__to_raw_pointer(--__end_));
                                                                   ^ ~~~~~~
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/vector:368:29: note: 
      in instantiation of member function
      'std::__1::__vector_base<PointXYZRGBAI,
      Eigen::aligned_allocator_indirection<PointXYZRGBAI> >::__destruct_at_end'
      requested here
    void clear() _NOEXCEPT {__destruct_at_end(__begin_);}
                            ^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/vector:451:9: note: 
      in instantiation of member function
      'std::__1::__vector_base<PointXYZRGBAI,
      Eigen::aligned_allocator_indirection<PointXYZRGBAI> >::clear' requested
      here
        clear();
        ^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/iterator:1244:75: note: 
      in instantiation of member function
      'std::__1::__vector_base<PointXYZRGBAI,
      Eigen::aligned_allocator_indirection<PointXYZRGBAI> >::~__vector_base'
      requested here
  ...<class _Tp, class _Alloc> friend class _LIBCPP_TYPE_VIS_ONLY vector;
                                                                  ^
/Users/wfehrnstrom/Demeter/LidarFile.h:20:7: note: in instantiation of member
      function 'pcl::PointCloud<PointXYZRGBAI>::~PointCloud' requested here
class LidarFile{
      ^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/memory:1527:14: note: 
      in instantiation of function template specialization
      'std::__1::allocator_traits<std::__1::allocator<LidarFile>
      >::__destroy<LidarFile>' requested here
            {__destroy(__has_destroy<allocator_type, _Tp*>(), __a, __p);}
             ^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/vector:424:25: note: 
      in instantiation of function template specialization
      'std::__1::allocator_traits<std::__1::allocator<LidarFile>
      >::destroy<LidarFile>' requested here
        __alloc_traits::destroy(__alloc(), _VSTD::__to_raw_pointer(--__end_));
                        ^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/vector:368:29: note: 
      in instantiation of member function 'std::__1::__vector_base<LidarFile,
      std::__1::allocator<LidarFile> >::__destruct_at_end' requested here
    void clear() _NOEXCEPT {__destruct_at_end(__begin_);}
                            ^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/vector:451:9: note: 
      in instantiation of member function 'std::__1::__vector_base<LidarFile,
      std::__1::allocator<LidarFile> >::clear' requested here
        clear();
        ^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/iterator:1244:75: note: 
      in instantiation of member function 'std::__1::__vector_base<LidarFile,
      std::__1::allocator<LidarFile> >::~__vector_base' requested here
  ...<class _Tp, class _Alloc> friend class _LIBCPP_TYPE_VIS_ONLY vector;
                                                                  ^
/Users/wfehrnstrom/Demeter/PointXYZRGBAI.h:9:8: note: forward declaration of
      'PointXYZRGBAI'
struct PointXYZRGBAI;
       ^
In file included from /Users/wfehrnstrom/Demeter/core.cpp:9:
In file included from /usr/local/include/boost/thread/thread.hpp:12:
In file included from /usr/local/include/boost/thread/thread_only.hpp:17:
In file included from /usr/local/include/boost/thread/pthread/thread_data.hpp:24:

任何帮助或澄清将不胜感激。

【问题讨论】:

  • 在不知道 pcl::PointCloud 模板的全部内容的情况下,最可能的答案是该模板试图实例化其参数类的实例,或者以某种需要类的方式使用它已定义。
  • 一个 typedeclared。它的成员实现的。在标题中声明您的结构。
  • 是的!或递增/递减/减去指针:因为必须知道大小

标签: c++ struct instance forward-declaration point-cloud-library


【解决方案1】:

你可以用不完整的类型做很少的事情。

如果你像你一样转发声明structclass,你只能声明一个指向这样一个对象的指针:

  • 您甚至不能递增或递减这样的指针,因为这需要编译器知道对象的大小(即知道它的声明)。
  • 您也不能分配新对象,因为构造函数未知,对齐要求也未知。

所以你必须将结构定义放在标题中:

#define PCL_NO_PRECOMPILE

#ifndef POINTXYZRGBAI_H
#define POINTXYZRGBAI_H

#include <pcl/point_types.h>

struct PointXYZRGBAI{
  PCL_ADD_POINT4D;  // macro in pcl/point_types.h that defines some member functions
  union{
    struct{
      float intensity;
      uint32_t rgba;
    };
    float data_c[4];
  };
  EIGEN_MAKE_ALIGNED_OPERATOR_NEW  // eigen/core seems included in pcl/point_types.h 
} EIGEN_ALIGN_16;

inline std::ostream& operator << (std::ostream& os, const PointXYZRGBAI& p);

#endif

函数(和注册宏)的实现应保留在 cpp 文件中。

【讨论】:

  • PLC_ADD_POINT4D 是一种添加 8 个成员函数的技巧,这些函数构造一些对象并按值返回它们。您是否在链接说明中添加了相应的目标文件或库文件?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-04-30
  • 1970-01-01
  • 2021-09-01
  • 2012-04-23
  • 2021-12-31
  • 2012-08-15
相关资源
最近更新 更多