2016-04-13 6 views
1

私はポリゴン計算を行うには、仮想関数を使用してプログラムを書くつもりが、私はプログラムを終了した後、BLOCK_TYPE_IS_VALIDありますよ(PHEAD - > nBlockUse)エラーBLOCK_TYPE_IS_VALID(PHEAD - > nBlockUse)エラー

// pointers to base class 
#include <iostream> 
#include "Polygon.h" 
#include "Rectangle.h" 
#include "Triangle.h" 

using namespace std; 

int main() { 
Rectangle rect1(4, 5); 
Rectangle rect2(3, 3); 
Triangle tri(4, 4, false); 
int triLength[3] = { 5, 4, 3 }; 
tri.setsideLength(triLength); 

Polygon * p = &rect1; 
cout << "Rectangle 1: " << endl; 
cout << "\tArea: " << p->area() << endl; 
cout << "\tSide: "; 
p->printsideLength(); 
rect1.printsideLength(); 
cout << "\tTotal Side Length: " << p->totalsideLength() << endl; 
p = &rect2; 
cout << "Rectangle 2: " << endl; 
cout << "\tArea: " << p->area() << endl; 
cout << "\tSide: "; 
p->printsideLength(); 
rect2.printsideLength(); 
cout << "\tTotal Side Length: " << p->totalsideLength() << endl; 
p = &tri; 
cout << "Triangle: " << endl; 
cout << "\tArea: " << p->area() << endl; 
cout << "\tSide: "; 
p->printsideLength(); 
cout << "\tTotal Side Length: " << p->totalsideLength() << endl; 
system("pause"); 
return 0; 
} 

ここではプログラムのmain.cppですが、プログラムは何も入力する必要はなく、結果を表示する必要があります。ここ

三クラス(CPP & H)

#ifndef Polygon_H 
#define Polygon_H 

#include <iostream> 
using namespace std; 

class Polygon{ 
private: 
int noOfSide; 
bool isAllSideEqual; 
int* sideLength; 
public: 
Polygon(); 
Polygon(int n,bool s); 
~Polygon(); 
void setsideLength(int* sl); 
void printsideLength(); 
int totalsideLength(); 
virtual int area(); 
}; 

#endif 

あります。

#include "Polygon.h" 
#include <iostream> 
using namespace std; 

Polygon::Polygon(){ 
    noOfSide = 3; 
    isAllSideEqual = false; 
    sideLength = new int[noOfSide]; 
    sideLength = &sideLength[noOfSide]; 
}; 


Polygon::Polygon(int n,bool s){ 
    if (n<3) 
    { 
     noOfSide = 3; 
     isAllSideEqual = false;} 
    else 
    { 
     noOfSide = n; 
     isAllSideEqual = s; 
    }; 
    sideLength = new int[noOfSide]; 
    sideLength = &sideLength[noOfSide]; 

}; 

Polygon::~Polygon(){ 
    delete[] sideLength; 
}; 


void Polygon::setsideLength(int* sl){ 
    for(int i=0;i<noOfSide;i++) 
     sideLength[i] = sl[i]; 
}; 
void Polygon::printsideLength(){ 
    for(int i=0;i<noOfSide;i++) 
     cout << sideLength[i] <<" "; 
}; 

int Polygon::totalsideLength(){ 
    int total = 0; 
    for(int i=0;i<noOfSide;i++) 
     total += sideLength[i]; 
    return total; 
}; 
int Polygon::area(){ 
    return 0; 
}; 

    #ifndef Triangle_H 
    #define Triangle_H 

    #include <iostream> 
    #include "Polygon.h" 
    using namespace std; 

class Triangle:public Polygon 
{ 
private: 
    int width; 
    int height; 
public: 
    Triangle(int w,int h,bool s); 
    virtual int area(); 
}; 
#endif 


#include "Triangle.h" 
#include <iostream> 
    using namespace std; 
Triangle::Triangle(int w,int h,bool s){ 
    width = w; 
    height = h; 
    Polygon(3,s); 
}; 

int Triangle::area(){ 
    int total = 0; 
    total = (width*height)/2; 
    return total; 
}; 


    #ifndef Rectangle_H 
    #define Rectangle_H 

    #include <iostream> 
    #include "Polygon.h" 
    using namespace std; 

    class Rectangle:public Polygon{ 
    private: 
    int width; 
    int height; 
public: 
    Rectangle(int w,int h); 
    void printsideLength(); 
    virtual int area(); 
}; 
#endif 

#include "Rectangle.h" 
#include <iostream> 
using namespace std; 

Rectangle::Rectangle(int w,int h){ 
    width=w; 
    height=h; 
    if(width = height) 
     Polygon(4,true); 
    else 
     Polygon(4,false); 
    int* size =new int[4]; 
    size[0] = width; 
    size[1] = height; 
    size[2] = width; 
    size[3] = height; 
    Polygon::setsideLength(size); 
}; 

void Rectangle::printsideLength(){ 
    for(int i=0;i<2;i++) 
     cout<< width<<" "<<height <<" "; 
}; 

int Rectangle::area(){ 
    int total =0; 
    total = width*height; 
    return total; 
}; 

プログラムは、コンパイル・エラーなしなので、唯一の問題は、メモリ についてですが、どこが間違っていますか?仮想関数の部分が間違っているか、他の部分ですか?

答えて

1

いくつかの問題があります。あなたのPolygonコンストラクタで

sideLength = new int[noOfSide]; 
sideLength = &sideLength[noOfSide]; 

の2行目はメインのバグです。
sideLengthは、ちょうど割り当てたメモリの最後を指します。
このポインタの使用は未定義です。
あなたはその場所に後でコピーし、両方とも無効なdeleteに渡しています。
sideLength = new int[noOfSide];のみ必要です。
クラスが手動でメモリを管理しているので、適切なコピーコンストラクタと代入演算子も必要です。

(あなた本当には何をすべきstd::vector<int>を使用して、メモリの割り当てを心配停止です。)

あなたのサブクラスのコンストラクタで、Polygon(3,s);などは、あなたの基本クラスを初期化していない、彼らは無名のPolygonいるの作成すぐに破棄される。

あなたは(あなたの他のメンバーと一緒に)初期化子リスト内の基底クラスを初期化する必要があります

Triangle::Triangle(int w, int h, bool s) 
    : Polygon(3, s), 
    width(w), 
    height(h) 
{ 
} 

Rectangleのコンストラクタはどこ、あなたの使用して割り当て、=の追加問題で、同じ問題を持っています等価、==を使用する必要があります。 sidelength部分の (あなたのコンパイラは、あなたがその警告を有効にしています。これについて警告し、それらに耳を傾けることができます。)

Rectangle::Rectangle(int w, int h) 
    : Polygon(4, w == h), 
    width(w), 
    height(h) 
{ 
    int size[] = {width, height, width, height}; 
    setsideLength(size); 
} 
+0


は、プログラムサイズはに等しいと動的な整数配列を作成する必要があります辺の数を計算し、配列のアドレスをポインタsideLengthに格納します。 –

+0

この要件を満たすには、新しい配列を作成してsideLengthにアドレスを割り当てる必要がありますか? int *のように=新しいint [noOfSide]; sideLength =&array? –

+0

@TomFong "新しい配列を作成してからsideLengthにアドレスを代入する"というのは、sideLength = new int [noOfSide];ということとまったく同じです。 – molbdnilo

関連する問題