게시판 즐겨찾기
편집
드래그 앤 드롭으로
즐겨찾기 아이콘 위치 수정이 가능합니다.
뭐가 문제인지 모르겠어요.
게시물ID : programmer_9053짧은주소 복사하기
작성자 : 태초령
추천 : 0
조회수 : 264회
댓글수 : 2개
등록시간 : 2015/04/02 12:06:57
밑에 보시면 빨간줄 쳐진부분에서oparr 이 오류나있습니다. 근데 무엇이 문제인지 잘 모르겠네요... 어디를 어떻게 선언해야 할지도 모르겠구요.
좀 도와주세요.
// 메인 파일------------------------------------------------------------------------------------------------
#include <iostream>
#include "cpp5.h"

using std::cout;
using std::cin;
using std::endl;

template < typename T>
class BoundCheckArray
{
private:
T* arr;
int arrlen;

BoundCheckArray(const BoundCheckArray& arr) { }
BoundCheckArray& operator = ( const BoundCheckArray& arr) { }

public:
BoundCheckArray(int len);
T& operator[] (int idx);
T operator[] (int idx) const;
int GetArrLen() const;
~BoundCheckArray();
};

template <typename T>
BoundCheckArray<T> :: BoundCheckArray(int len) : arrlen(len) 
{
arr=new T[len];
}

template <typename T>
T& BoundCheckArray<T> :: operator[] (int idx)
{
if(idx < 0 || idx>=arrlen)
{
cout<<"Array index Out of bound exception"<<endl;
exit(1);
}
return arr[idx];
}

template <typename T>
T BoundCheckArray<T> :: operator[] (int idx) const
{
if(idx < 0 || idx>=arrlen)
{
cout<<"Array index Out of bound exception"<<endl;
exit(1);
}
return arr[idx];
}

template <typename T>
int BoundCheckArray<T> :: GetArrLen() const
{
return arrlen;
}

template <typename T>
BoundCheArray::~BoundCheckArray()
{
delete[] arr;
}

void main()
{
BoundCheckArray<Point<int>> oarr1(3);
oarr1[0]=Point<int>(3,4);
oarr1.operator[](1) = Point<int>(5,6);
oarr1[2]=Point<int>(7,8);
for(int i=0; i<oarr1.GetArrLen(); i++)
oarr1[i].ShowPosition();

BoundCheckArray<Point<double>> oarr2(3);
oarr2[0] = Point<double>(3.14, 4.31);
oarr2[1] = Point<double>(5.09, 6.07);
oarr2[2] = Point<double>(7.82, 8.54);

for(int i=0; i<oarr2.GetArrLen(); i++)
oarr2[i].ShowPosition();

typedef Point<int>* POINT_PTR;
BoundCheckArray<POINT_PTR> oparr(3);
oparr[0]=new Point<int>(11,12);
oparr[1]=new Point<int>(13,14);
oparr[2]=new Point<int>(15,16);

for(int i=0; i<oparr.GetArrLen(); i++)
oparr[i].ShowPosition();

delete oparr[0];
delete oparr[1];
delete oparr[2];
}

// class point 헤더파일-------------------------------------------------------------------------------------------
#ifndef __CPP5_H_
#define __CPP5_H_

template <typename T>
class Point
{
private:
T xpos, ypos;
public:
Point ( T x=0, T y=0);
void ShowPosition() const;
};
#endif

//class point 소스파일-------------------------------------------------------------------------------------------
#include <iostream>
#include "cpp5.h"
using std::cout;
using std::cin;
using std::endl;

template <typename T>
Point<T>::Point(T x, T y) : xpos(x), ypos(y) { }

template <typename T>
void Point<T>::ShowPosition() const
{
cout<<'['<<xpos<<", "<<ypos<<']'<<endl;
}
//----------------------------------------------------------------------------------------------------------
전체 추천리스트 보기
새로운 댓글이 없습니다.
새로운 댓글 확인하기
글쓰기
◀뒤로가기
PC버전
맨위로▲
공지 운영 자료창고 청소년보호