2012-04-16 5 views
1

확인, 그래서 여기 내 헤더 파일 (또는 적어도 일부)입니다 :클래스 템플릿, 예상 생성자, 소멸자

template<class T> 

class List 
{ 
public: 
. 
: 
List& operator= (const List& other); 
. 
: 
private: 
. 
: 
}; 

여기 내 .CC 파일입니다 다음에

template <class T> 
List& List<T>::operator= (const List& other) 
{ 
    if(this != &other) 
    { 
     List_Node * n = List::copy(other.head_); 
     delete [] head_; 
     head_ = n; 
    } 
    return *this; 
} 

line List& List<T>::operator= (const List& other) 컴파일 오류가 발생합니다. "& '토큰"이전에 예상되는 생성자, 소멸자 또는 형식 변환이 발생합니다. 여기서 내가 뭘 잘못하고 있니?

+0

템플릿 클래스 정의는 헤더 파일에 있어야합니다. 설명을 위해이 질문을보십시오 : http://stackoverflow.com/questions/495021/why-can-templates-only-be-implemented-in-the-header-file –

답변

3

반환 형식 List&은 템플릿 인수없이 사용할 수 없습니다. List<T>&이어야합니다.

template <class T> 
List<T>& List<T>::operator= (const List& other) 
{ 
    ... 
} 


그러나 당신이이 구문 오류를 수정 후에도 템플릿 함수 정의를 헤더 파일에 배치 할 필요가 있기 때문에, 당신은 링커 문제가 있습니다 있습니다. 자세한 내용은 Why can templates only be implemented in the header file?을 참조하십시오.

관련 문제