C++Builder Programming Forum
C++Builder  |  Delphi  |  FireMonkey  |  C/C++  |  Free Pascal  |  Firebird
볼랜드포럼 BorlandForum
 경고! 게시물 작성자의 사전 허락없는 메일주소 추출행위 절대 금지
C++빌더 포럼
Q & A
FAQ
팁&트릭
강좌/문서
자료실
컴포넌트/라이브러리
메신저 프로젝트
볼랜드포럼 홈
헤드라인 뉴스
IT 뉴스
공지사항
자유게시판
해피 브레이크
공동 프로젝트
구인/구직
회원 장터
건의사항
운영진 게시판
회원 메뉴
북마크
볼랜드포럼 광고 모집

C++빌더 Q&A
C++Builder Programming Q&A
[71264] iterator 와 list class 구현중 질문입니다.!!
이민규 [] 3725 읽음    2014-04-30 20:26
C++ 형식으로 작성했는데 출력이 이상하게 나오네용

template
<typename T>
이렇게 썻습니다.!!

#pragma once
#include "DoublySortedLinkedList.h"

template 
class Iterator
{
public:
	// Class constructor
	Iterator(const DoublySortedLinkedList &list): m_List(list), m_pCurrentPos(list.m_pFirst){};
	bool NotNull();							// list의 현재원소가 Null이 아닌지 검사
	bool NextNotNull();						// list의 다음원소가 Null이 아닌지 검사
	T First();								// list의 처음 node의 item을리턴
	T Next();								// 다음 node로 이동하고 해당node의 item을리턴
	NodeType GetCurrentNode();			// 현재 node를 리턴
private:
	const DoublySortedLinkedList &m_List;	//사용할리스트의참조변수
	NodeType *m_pCurrentPos;					// Iterator
	friend class DoublySortedLinkedList;
}

----------------------------------------------------------------------------------------------

#pragma once
#include "Iterator.h"

template 
struct NodeType;

template 
class DoublySortedLinkedList
{
public:
	DoublySortedLinkedList();					// Class constructor
	~DoublySortedLinkedList();					// Class destructor
	bool IsFull();								// Check the memory
	void MakeEmpty();							// Initializes list to empty state.
	void InsertItem(T item);					// Add item to list.
	void DeleteItem(T item);					// Delete item from list.
	void ReplaceItem(T item);					// Replace item in list.
	int GetLength() const;						// Determines the number of elements in list.
	void RetrieveItem(T &item, bool&found);		// Retrieves list element whose key matches item's key (if present).

private:
	NodeType *m_pFirst;	// 리스트의처음노드를가리키는포인터
	NodeType *m_pLast;	// 리스트의마지막노드를가리키는포인터
	int m_nLength;
	friend class Iterator;
};

/*---------------------------------------------------------
NodeType Structure
아이템, 앞을 가리키는 포인터, 뒤를 가리키는 포인터
3가지 변수를 갖는다.
----------------------------------------------------------*/
template
struct NodeType
{
	T item;					// 데이터가 저장될 item
	NodeType *prevPtr;	// Pointer of previous node
	NodeType *nextPtr;	// Pointer of next node
};

//Constructor
template
DoublySortedLinkedList::DoublySortedLinkedList()
{
	*m_pFirst = NULL;	// 리스트의처음노드를가리키는포인터
	*m_pLast = NULL;	// 리스트의마지막노드를가리키는포인터
	m_nLength = 0;
}

//Destructor
template
DoublySortedLinkedList::~DoublySortedLinkedList()
{
	//리스트 내의 모든 Node 제거
	MakeEmpty();
}


//====================================================================================
// Function	: Initializes list to empty state.
// Pre		: None
// Post		: List is empty.
//====================================================================================
template
void DoublySortedLinkedList::MakeEmpty()
// Post: List is empty; all items have been deallocated.
{
	// 리스트내의 모든 노드 제거 하고 리스트의 length를 초기화
	NodeType *temp;
	while (m_pListData != NULL)
	{
		temp = m_pListData;
		m_pListData = m_pListData->next;
		delete temp;
	}
	m_nLength = 0;
}

//====================================================================================
// Function	: Determines the number of elements in list.
// Post		: Function value = number of elements in list.
//====================================================================================
template 
int DoublySortedLinkedList::GetLength() const
{
	return m_nLength;
}


//====================================================================================
// Function	: Adds item to list.
// Pre		: none
// Post		: item is in list.
//====================================================================================
template
void DoublySortedLinkedList::InsertItem(T item)
{
	if ( !IsFull() )	// check dynamic allocation of node
	{
		NodeType *node = new NodeType;		// 추가될 노드
	
		node->item = item;
		node->nextPtr = NULL;
		node->prevPtr = NULL;

		if (!m_nLength)	// 길이가 0인 경우
		{
			m_pListData = node; // 리스트의 첫번째 데이터에 node를 삽입
		}
		else
		{	
			//	Iterator 를 사용하지 않고 그냥 ??
			//	Iterator class를 이용하는건지 ??
		}
		m_nLength++;
	}
}

//====================================================================================
// Function	: Delete item from list.
// Pre		: none
// Post		: item is not in list.
//====================================================================================
template
void DoublySortedLinkedList::DeleteItem(T item)
{
	if ( m_nLength > 0 )	// 길이가 0보다 큰 경우
	{
		// 삭제 알고리즘
		// 미구현
	}
	cout << "\tNot Found Item" << endl;	// 아이템을 찾지 못한경우 메세지 출력
}

//====================================================================================
// Function	: Replace item in list.
// Pre		: none
// Post		: item is in list and updated
//====================================================================================
template
void DoublySortedLinkedList::ReplaceItem(T item)
{
	bool found;
	T temp = item;
	RetrieveItem(temp, found);	// item을 검색한다.
	if (found)	// 찾게되면 repalce
	{
		m_pCurrentPos->item = item;
		cout << "\tUPDATE SUCCESS" << endl;
	}
	else	// 찾지 못하면 메세지 출력
	{
		cout << "\tNot Found Item!" << endl;
	}
}

//====================================================================================
// Function	: Reterieves item from list.
// Pre		: none
// Post		: 찾을 경우 m_pCurPointer가 찾고자 하는 아이템을 가리킨다. found = true 가 된다.
//			  찾지 못할 경우 found = false;
//====================================================================================
template
void DoublySortedLinkedList::RetrieveItem(T& item, bool& found)
{
	T temp;
	ResetList();
	found = false;

	if ( m_nLength > 0 )
	{
		// 검색 알고리즘
		// 미구현
	}
}


이렇게 했는데 자꾸 컴파일 에러가 뜨네요....ㅠㅠㅠ이유를 모르겠습니다.


Error 21 error C1903: unable to recover from previous error(s); stopping compilation
Error 42 error C1903: unable to recover from previous error(s); stopping compilation
Error 14 error C2059: syntax error : '<'
Error 19 error C2059: syntax error : '<'
Error 35 error C2059: syntax error : '<'
Error 40 error C2059: syntax error : '<'
Error 3 error C2143: syntax error : missing ',' before '<'
Error 24 error C2143: syntax error : missing ',' before '<'
Error 4 error C2143: syntax error : missing ';' before '<'
Error 8 error C2143: syntax error : missing ';' before '<'
Error 10 error C2143: syntax error : missing ';' before '<'
Error 25 error C2143: syntax error : missing ';' before '<'
Error 29 error C2143: syntax error : missing ';' before '<'
Error 31 error C2143: syntax error : missing ';' before '<'
Error 1 error C2143: syntax error : missing ';' before 'template'
Error 22 error C2143: syntax error : missing ';' before 'template'
Error 6 error C2238: unexpected token(s) preceding ';'
Error 9 error C2238: unexpected token(s) preceding ';'
Error 12 error C2238: unexpected token(s) preceding ';'
Error 15 error C2238: unexpected token(s) preceding ';'
Error 27 error C2238: unexpected token(s) preceding ';'
Error 30 error C2238: unexpected token(s) preceding ';'
Error 33 error C2238: unexpected token(s) preceding ';'
Error 36 error C2238: unexpected token(s) preceding ';'
Error 13 error C2327: 'Iterator<T>::DoublySortedLinkedList' : is not a type name, static, or enumerator
Error 34 error C2327: 'Iterator<T>::DoublySortedLinkedList' : is not a type name, static, or enumerator
Error 20 error C2588: '::~DoublySortedLinkedList' : illegal global destructor
Error 41 error C2588: '::~DoublySortedLinkedList' : illegal global destructor
Error 18 error C2988: unrecognizable template declaration/definition
Error 39 error C2988: unrecognizable template declaration/definition
Error 16 error C2989: 'DoublySortedLinkedList' : class template has already been declared as a non-class template
Error 37 error C2989: 'DoublySortedLinkedList' : class template has already been declared as a non-class template
Error 17 error C3857: 'DoublySortedLinkedList': multiple template parameter lists are not allowed
Error 38 error C3857: 'DoublySortedLinkedList': multiple template parameter lists are not allowed
Error 2 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
Error 5 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
Error 7 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
Error 11 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int

Error 23 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
Error 26 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
Error 28 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
Error 32 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int

처음보는 에러인데...당황스럽네요..ㅠㅠ
어떻게 해야 하나요..



아 그리구 위의 저런 ADT 를 가지고 구현할때요 이때 list class 에서

insert 나 retrieve 메소드를 구현함에 friend 선언이 된 iterator를 내부적으로 생성해서 사용하는건가요??

+ -

관련 글 리스트
71264 iterator 와 list class 구현중 질문입니다.!! 이민규 3725 2014/04/30
71349     Re:iterator 와 list class 구현중 질문입니다.!! 남병철.레조 3270 2014/05/21
Google
Copyright © 1999-2015, borlandforum.com. All right reserved.