正在尝试使用对象指针的boost::multi_array

Trying to use boost::multi_array of pointer of object

本文关键字:boost multi array 指针 对象      更新时间:2023-10-16

我面临以下问题。我想使用boost::multi_array创建一个对象的多维指针数组,但即使我编写的代码是编译的,当我尝试在Eclipse中运行时,程序也会终止,并且不会打印任何内容。让我举一个很小的例子,以防这有任何帮助。因此拥有以下非常小的简单类:

class example {
     public:
            example();
            virtual ~example();
            int a;
};

我只是尝试以以下方式创建并使用这个类的指针数组:

int main() {
           typedef boost::multi_array<example * , 2> array_type1;
           array_type1 DE(boost::extents[2][2]);
           DE[0][0]->a=6;
           DE[1][0]->a=7;
           DE[0][1]->a=8;
           DE[1][1]->a=9;
cout << "!!!Hello World!!!" << endl; // prints !!!Hello World!!!
return 0;

}

还要注意,当我使用boost/test/minimal.hpp运行相同的代码时(http://www.boost.org/doc/libs/1_46_1/libs/test/doc/html/minimal.html)为了检查正在发生的事情,结果主要看起来是这样的:

int test_main(int, char*[]){

           typedef boost::multi_array<example * , 2> array_type1;
           array_type1 DE(boost::extents[2][2]);
           DE[0][0]->a=6;
           DE[1][0]->a=7;
           DE[0][1]->a=8;
           DE[1][1]->a=9;
cout << "!!!Hello World!!!" << endl; // prints !!!Hello World!!!
return boost::exit_success;

}

我收到以下消息:

/usr/include/boost/test/minimal.hpp(123): exception "memory access violation at address: 0x00000008: no mapping at fault address" caught in function: 'int main(int, char**)'
**** Testing aborted.
**** 1 error detected

任何关于如何解决这个问题的建议现在都会对我很有帮助!

array_type1 DE(boost::extents[2][2]);
DE[0][0]->a=6;

您在DE[0][0]处取消引用指针,但事先从未使其指向实际的example实例。