|
오늘 VC++6.0 Enterprise를 구해서 컴파일해본 결과,
CPoly 클래스의 생성자에 있는 아래 코드를 지우고 하면 아무 오류 안나고 잘 되는데요.
if ( points != NULL ) // NULL이 아닌 경우 메모리 해제
delete [] points;
빛살 님이 쓰신 글 :
: 조금 긴거지만 꼭 부탁드립니다....^^
:
: #include <iostream>
: using namespace std;
:
: class POINT //x,y의 좌표 설정 클래스
: {
: public:
: int x,y;
: };
:
: class CIntVector2 : public POINT // POINT를 상속받아 벡터형으로 표현
: {
: public:
: CIntVector2( int _x = 0, int _y = 0 ); //생성자
: ~CIntVector2(); //소멸자
: CIntVector2 operator +( const CIntVector2& intVector2 ); //+연산자 오버로딩
: CIntVector2 operator -( const CIntVector2& intVector2 ); //-연산자 오버로딩
: void operator()( int _x, int _y ); //()를 오버로딩
: };
:
: CIntVector2::CIntVector2( int _x, int _y )
: {
: x = _x;
: y = _y;
: }
:
: CIntVector2::~CIntVector2()
: {}
:
: CIntVector2 CIntVector2::operator +( const CIntVector2& intVector2 )
: {
: return CIntVector2( x + intVector2.x, y + intVector2.y );
: }
:
: CIntVector2 CIntVector2::operator -( const CIntVector2& intVector2 )
: {
: return CIntVector2( x - intVector2.x, y - intVector2.y );
: }
:
:
: void CIntVector2::operator()( int _x, int _y )
: {
: x = _x;
: y = _y;
: }
:
: class CPoly //다각형을 구현하기 위한 클래스
: {
: public:
: CIntVector2* points; //점들의 집합을 나타내는 포인터
: int edge; //선분의 갯수
:
: CPoly();
: CPoly( CIntVector2* _points, int _edge ); //배열과 선분의 갯수를 받는생성자
: CPoly( const CPoly& _poly ); //복사생성자
: ~CPoly();
: };
:
: CPoly::CPoly() //디폴트 생성자
: {
: points = NULL; //널로 설정
: edge = 0; //0으로 설정
: }
:
: CPoly::CPoly( CIntVector2* _points, int _edge )
: {
: edge = _edge; //선분 대입
:
: if ( points != NULL ) // NULL이 아닌 경우 메모리 해제
: delete [] points;
:
: points = new CIntVector2[edge]; //선분갯수 만큼의 동적할당
:
: for ( int i = 0; i < edge; ++i ) { //인자로 받은 배열을 객체의 배열에 대입
: (points+i)->x = (_points+i)->x;
: (points+i)->y = (_points+i)->y;
: }
: }
:
: CPoly::CPoly( const CPoly& _poly )
: {
: edge = _poly.edge;
:
: if ( points != NULL )
: delete [] points;
:
: points = new CIntVector2[edge];
:
: for ( int i = 0; i < edge; ++i ) {
: (points+i)->x = (_poly.points+i)->x;
: (points+i)->y = (_poly.points+i)->y;
: }
: }
:
: CPoly::~CPoly() //소멸자
: {
: if ( points != NULL )
: delete [] points;
: }
:
: int main(int argc, char *argv[])
: {
: CIntVector2 array[5]; //5개의 점을 생성
: array[0](100,100); //각점들의 좌표 대입
: array[1](200, 50);
: array[2](300,100);
: array[3](250,200);
: array[4](150,200);
:
: CPoly poly1( array, 5 ); //좌표들을 가진 배열과 선분의 숫자 입력 <- 에러줄..ㅡ.ㅡ;;
:
: for ( int i = 0; i < 5; ++i ) { // 시험삼아 객체에 있는 좌표들 출력
: cout << poly1.points[i].x << " " << poly1.points[i].y << endl;
: }
:
: return 0;
: }
:
: dev-cpp와 빌더로 컴파일 하면 에러 없이 결과 값을 보여주는데
:
: 비쥬얼 C++로 하면 컴파일시는 에러가 없는데 실행동안에 메모리 참조 오류가
:
: 나는군요....
:
: 단지 컴파일러 문제인가요....아님 제가 잘못짠것인가요...
:
: 좋은 답변 부탁드립니다.
|