How can I know what makes an incomplete type incomplete for error: arithmetic on a pointer to an incomplete type

How can I know what makes an incomplete type incomplete for error: arithmetic on a pointer to an incomplete type

我试图定义我自己的 PointT 类型,以便在单个点中为我提供 XYZRGBA 和强度值。我尝试遵循点云库网站上这篇文章中规定的约定:http://pointclouds.org/documentation/tutorials/adding_custom_ptype.php

PointXYZRGBAI 的实现在我的 类 之一的 .CPP 文件中,但我在其他 header 文件包含的 header 文件中转发声明它。实现在 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);
}

并且前向声明在 header 文件名中 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 的指针上进行算术运算,就像当我声明一个使用结构的点云时PointXYZRGBAI:

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 header 之前实现该结构?仅供参考,这里是完整的错误堆栈跟踪:

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:

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

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

如果像以前一样转发声明 structclass,则只能声明指向此类 object 的指针:

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

所以你必须把结构定义放在 header:

#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 文件中。