【问题标题】:boost multiarray of user defined class object提升用户定义的类对象的多数组
【发布时间】:2014-03-06 18:41:58
【问题描述】:

我需要描述一个二维对象类的 boost 多数组,即 myclass。是否可以?或者只能定义标准类型为 int、double 等的多数组

提前感谢您的回答

【问题讨论】:

  • 你不能写 boost::multi_array

标签: c++ boost


【解决方案1】:

是的,它是一个类模板。它旨在取代 std::vector 嵌套,并使用更少的内存。 boost::multi_array<myclass, 2>,创建myclass的二维数组。

【讨论】:

  • 是的,我尝试定义
【解决方案2】:

与“原生”C++ 数组不同,boost 多数组具有值语义。

您的元素类型必须是可复制的,因为multi_array 承诺是可复制的:

multi_array

...

型号。* MultiArray, CopyConstructible。根据元素类型,它还可以建模EqualityComparableLessThanComparable

比较以下:

#include <boost/multi_array.hpp>

using Arr = boost::multi_array<int, 3>;

struct Ok { };
struct NotOk : boost::noncopyable { };

int main()
{
    boost::multi_array<int,   3> arr1(boost::extents[7][6][3]); // ok
    boost::multi_array<Ok,    3> arr2(boost::extents[7][6][3]); // ok
    boost::multi_array<NotOk, 3> arr3(boost::extents[7][6][3]); // COMPILE ERROR
}

On Coliru

【讨论】:

    【解决方案3】:

    是的,我尝试过

    typedef boost::multi_array<Pixel, 2> pixel_2d_t;
    

    (Pixel是我的类)并在头文件Pixel.h中定义Pixel

    class Pixel:
        public IRDetectorComponent<Pixel>::Type
        ,public det::IRPositionable<Pixel>
        {
        public:
    
            static const char* const kComponentName;
    
            static const char* const kComponentId;
            /**
             *   Pixel Status definition
             */
            enum Status {
              eGood = 0,
              eBadCalibration = 1,
              eUnknown
            };
    

    ... ...

    但我得到错误

    /home/lperal/jemeuso/jape/External/boost/1_55_0/include/boost/multi_array/base.hpp:139:25:错误:指向不完整类型的指针的算术 'irdet::CCD::像素' TPtr newbase = base + idx * strides[0];

    作为 CCD.h 我在 irdet 命名空间中定义了多数组的类。

    【讨论】:

      猜你喜欢
      • 2013-12-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-11-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多