宿題のために作成しているプロジェクトに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;
}
ありがとう。