2010-05-15 2 views
0

내가 (INT, 부울, 문자열 등을위한)는 컨테이너 클래스 템플릿에서 일하고 있어요, 나는이 부분이 오류템플릿 경고 및 오류 도움말 (GCC)

cont.h:56: error: expected initializer before '&' token 

함께 붙어 있었어요

template <typename T> 
const Container & Container<T>::operator=(const Container<T> & rightCont){ 

정확히 내가 거기에서 잘못 했습니까?

이 경고 메시지의 의미를 모르겠습니다. 이 위치 첫 번째 경우에

template <typename T> 
class Container{ 
    friend bool operator==(const Container<T> &rhs,const Container<T> &lhs); 
public: 

답변

1

첫 번째 경우에는 거꾸로했습니다. 당신이 반환 유형을 지정하는 경우, 템플릿 식별자 (Container<T>)로 템플릿 매개 변수의 목록을 포함해야하지만, 당신은 매개 변수 유형을 지정하는 경우, 당신이 그것을 할 필요가 없습니다

template <typename T> 
const Container<T> & Container<T>::operator=(const Container & rightCont){ 
    ... 
(단지 Container 충분하다)

다른 이유가 있습니다.

두 번째 경우에 operator ==을 친구로 선언하면이 경우 operator ==은 일반적인 기능이라는 것을 경고합니다. 템플리트의 특수화가 될 수 없습니다. 나는. 클래스 Container<int>의 경우

bool operator==(const Container<int> &rhs, const Container<int> &lhs) { 
    // ... 
} 

은 (는) 친구가됩니다. 그러나 U == int에 대한 기능 템플릿

template <class U> 
bool operator==(const Container<U> &rhs, const Container<U> &lhs) { 
    // ... 
} 

의 전문화 하지Container<int>의 친구가 될 것입니다. 그것이 당신의 의도라면, 당신은 괜찮습니다.위의 템플릿의 특정 전문화 친구가하고 싶었 경우 에게 위의 템플릿의 모든 전문화 친구가하고 싶었다면

, 당신은

template <typename T> 
class Container { 
    friend bool operator==<T>(const Container<T> &rhs, const Container<T> &lhs); 
    ... 

말해야 할 것입니다, 당신은 할 것 예 :

template <typename T> 
class Container { 
    template <class U> 
    friend bool operator==(const Container<U> &rhs, const Container<U> &lhs); 
    ... 
2

에서

cont.h:13: warning: friend declaration `bool operator==(const Container<T>&, const Container<T>&)' declares a non-template function 
cont.h:13: warning: (if this is not what you intended, make sure the function template has already been declared and add <> after the function name here) -Wno-non-template-friend disables this warning 

, 당신은 대신 Container<T> 단지 Container 처음으로 말을하고 있습니다.

두 번째 경우에는 template을 반복해야한다고 생각합니다. 템플릿 클래스의 멤버 함수는 암시 적으로 템플리트 처리되지만 친구 함수는 반드시 필요하지는 않으므로보다 명시 적으로 처리해야합니다. 나는 당신이 그 목적에 필요한 정확한 어떤 구문 모르겠지만, 나는 생각 : 클래스 전

  • , 수업 시간에 template<typename T> bool operator==(
  • , bool operator==<>(

내가 그 생각 등 오류 메시지가 전달하려고하는 것, 비록 매우 명확하지는 않지만.