提升smart_ptr运行时错误

时间:2012-10-06 06:53:20

标签: c++ arrays boost runtime-error shared-ptr

我仍在尝试在我正在为家庭作业创建的项目中实施boost shared_ptr,并继续遇到不同的错误。目前,我觉得我的代码非常接近正确,并且它构建正常,但我遇到了一个讨厌的运行时错误。我只是想在下面的MyPrint函数中访问我的Shape类和Point类ToString函数。

我的代码如下:

#include "Point_H.hpp"
#include "Shape_H.hpp"
#include "Array_H.hpp"
#include "ArrayException_H.hpp"
#include "boost/shared_ptr.hpp"

using namespace CLARK::Containers;
using namespace CLARK::CAD;

class S1
{
private:
    boost::shared_ptr<Shape> sp;

public:
    S1(boost::shared_ptr<Shape> value) : sp(value) { cout << "S1 constructor call (default)" << endl; }
    virtual ~S1() { cout << "S1 destructor call" << endl; }
    virtual void print() const { cout << "Shape: " << (*sp).ToString() << endl; }
};

class P1
{
private:
    boost::shared_ptr<Point> pp;

public:
    P1(boost::shared_ptr<Point> value) : pp(value) { cout << "P1 constructor call (default)" << endl; }
    virtual ~P1() { cout << "P1 destructor call" << endl; }
    void print() const { cout << "Point: " << (*pp).ToString() << endl; }
};

void MyPrint()
{
    {
        boost::shared_ptr<Shape> myShape;
        {
            S1 Shape1(myShape);
            Shape1.print();
        }

        boost::shared_ptr<Point> myPoint;
        {
            P1 Point1(myPoint);
            Point1.print();
        }           
    }
}

int main()
{       
    // Typedef for a shared pointer to shape
    // a typedef for an array with shapes stored as shared pointers.
    typedef boost::shared_ptr<Shape> ShapePtr;
    typedef Array<ShapePtr> ShapeArray;

    ShapeArray my_ShapeArray(3);

    my_ShapeArray[0] = ShapePtr (new Point(1,2));

    MyPrint();

    try
    {   
    cout << my_ShapeArray[0]->ToString() << endl;

    return 0;       
    }

    catch(ArrayException& err)
    {
        cout << err.GetMessage() << endl;
    }    
}

命令窗口显示以下内容,运行时错误中止程序:

  

数组构造函数调用

     

S1构造函数调用(默认)

     

断言失败:px!= 0,文件c:\ program files x86)\ boost \ boost_1_51_0 \ boost \ smart_ptr \ shared_ptr.hpp,第418行

有人可以帮帮我吗?我一直试图调试这个好几个小时!

* 编辑:

按请求,数组默认构造函数:

Array默认构造函数:

template <typename Type> class Array
{
private:
Type* m_data; // dynamic array of Type objects
int m_size; // size of array
...
};

template <typename Type>
int Array<Type>::m_default_size = 10;

template <typename Type>
Array<Type>::Array()
{// Default constructor
    m_size = m_default_size;
    m_data = new Type[m_default_size];
    cout << "Array constructor call (default)" << endl;
}

感谢。

2 个答案:

答案 0 :(得分:0)

MyPrint中,您创建了myShape并且从不对其进行初始化。然后尝试在print调用中取消引用该指针,从而导致C ++模拟NullPointerException

您对此代码的期望有点不清楚。

答案 1 :(得分:0)

声明:

boost::shared_ptr<Shape> myShape;

Default构造一个指向Shape的共享指针,但这样做意味着它不引用实际的实例(在内部它是null)。相反,你需要将你的形状数组作为参数传递给MyPrint,如下所示:

void MyPrint(const Array<ShapePtr>& shapes)
{
    for (int i = 0; i < shapes.getSize(); ++i)
    {
        // this guard is necessary if you do not fully populate the array
        if (shapes[i])
        {
            shapes[i]->print();
        }
    }
}

在main中称之为:

MyPrint(my_ShapeArray);

请注意,您需要将此ShapePtr typedef移到main之外才能使用此更改。