私の知る限り、コンストラクタはインプリメンテーションファイルで定義する必要がありますが、クラスを持つサンプルを.hと.cppファイルに分割するのではなく、1つのメインファイル内で見つけることができました実装とヘッダのコンストラクタ
私の次のコードは、許容可能な方法で分離されている場合、私が知っておく必要があるのは..です
Entity.h:
using namespace std;
class cEntity {
private:
/*-----------------------------
----------Init Methods---------
-----------------------------*/
int *X, *Y;
int *Height, *Width;
public:
/*-----------------------------
----------Constructor----------
-----------------------------*/
cEntity (int,int, int, int);
/*-----------------------------
----------Destructor-----------
-----------------------------*/
~cEntity();
/*-----------------------------
----------Set Methods----------
-----------------------------*/
/*Set X,Y Methods*/
void setX(int x){*X=x;};
void setY(int y){*Y=y;};
void setXY(int x, int y){*X=x; *Y=y;};
/*Set Height, Width Methods*/
void setHeight(int x){*Height=x;};
void setWidth(int x){*Width=x;};
void setDimensions(int x, int y){*Height=x; *Width=y;};
/*-----------------------------
----------Get Methods----------
-----------------------------*/
/*Get X,Y Methods*/
int getX(){return *X;};
int getY(){return *Y;};
/*Get Height, Width Methods*/
int getHeight(){return *Height;};
int getWidth(){return *Width;};
};
とEntity.cpp:
#include "Entity.h"
cEntity::cEntity (int x, int y, int height, int width) {
X,Y,Height,Width = new int;
*X = x;
*Y = y;
*Height = height;
*Width = width;
}
cEntity::~cEntity() {
delete X, Y, Height, Width;
}
私はまた、特に私の最初の質問に非常に役立ってくれている皆様に感謝したいと思います!
boost.shared_ptr/boost.scoped_ptrを調べることをおすすめします。彼らは破壊時の削除を処理し、メモリリークのリスクは低くなります。また、そうしない理由がない限り、デストラクタを 'virtual'にしてください。 – moshbear
ポインタ以外の型を調べることをお勧めします。 – avakar
'delete X、Y、Height、Width;'これはあなたが思っていることをしません。 –