【问题标题】:Trying to use boost::multi_array of pointer of object尝试使用对象指针的 boost::multi_array
【发布时间】:2016-07-26 20:42:58
【问题描述】:

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

class example {
     public:
            example();
            virtual ~example();

            int a;

};

我只是尝试通过以下方式创建和使用该类的指针的 multi_array:

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

任何有关如何解决此问题的建议现在对我都非常有帮助!

【问题讨论】:

  • 我会从阅读好的book on C++ 开始,并且不使用指针

标签: c++ pointers object boost multidimensional-array


【解决方案1】:
array_type1 DE(boost::extents[2][2]);
DE[0][0]->a=6;

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-16
    • 1970-01-01
    • 2015-08-12
    • 1970-01-01
    • 2012-02-29
    相关资源
    最近更新 更多