如何知道错误的不完整类型是什么不完整:对指向不完整类型的指针的算术

时间:2016-09-03 18:15:36

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

我试图定义自己的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的指针进行算术运算,就像我声明使用结构的点云一样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头之前实现该结构?仅供参考,这是完整的错误堆栈跟踪:

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:

非常感谢任何帮助或澄清。

1 个答案:

答案 0 :(得分:2)

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

如果您转发声明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文件中。