0

宿題のために作成しているプロジェクトにboostshared_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

誰か助けてくれませんか?私はこれを何時間もデバッグしようとしてきました!

*編集:

リクエストごとに、配列のデフォルトコンストラクタ:

配列のデフォルトコンストラクタ:

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;
}

ありがとう。

4

2 に答える 2

0

ステートメント:

boost::shared_ptr<Shape> myShape;

デフォルトでは、Shapeへの共有ポインターが作成されますが、これを行うと、実際のインスタンスを参照しなくなります(内部的にはnullになります)。代わりに、Shapes配列をパラメーターとして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();
        }
    }
}

そしてそれを主にこのように呼んでください:

MyPrint(my_ShapeArray);

この変更を機能させるには、ShapePtrtypedefをmainの外に移動する必要があることに注意してください。

于 2012-10-06T07:57:36.807 に答える
0

In MyPrint you create myShape and never initialize it. You then try to dereference that pointer in the print call, causing the C++ analog of a NullPointerException.

It's a little unclear what you expect this code to do.

于 2012-10-06T07:16:47.073 に答える