2012-06-23 3 views
2

템플릿 클래스에 operator=을 오버로드하려고합니다.C++ 오류 C2801 : 'operator ='은 (는) 비 정적 멤버 여야합니다.

나는이 템플릿 클래스가 있습니다

template <class T> 
class Matrice 
{ 
    T m,n; 
public: 
    template <class V> 
    friend Matrice<V>& operator=(const Matrice<V> &); 
}; 

template <class T> 
Matrice<T>& Matrice<T>::operator=(const Matrice<T> &M) 
{ 
    /*...*/ 
    return *this; 
} 

을 나는 또한 시도 :

template <class T> 
class Matrice 
{ 
    T m,n; 
public: 
    template <class V> 
    Matrice<V>& operator=(Matrice<V> &); 
}; 

template <class T> 
Matrice<T>& operator=(Matrice<T> &M) 
{ 
    /*...*/ 
    return *this; 
} 

하지만, 난 여전히이 오류를 얻을 :

error C2801: 'operator =' must be a non-static member 
+0

두 번째 예는'friend' 문제를 해결할 수 있지만 해상도가 없습니다. – chris

+0

오류 메시지를 읽었습니까? –

+0

@Griwes 아무도 내 질문에 맞지 않을 때 개선 할 수 없습니다. –

답변

5

error C2801: 'operator =' must be a non-static member

굵게 표시된 단어입니다 여기 열쇠 야. friend 님은 회원이 아닙니다. 친구 야. 그 friend 키워드를 제거하고 회원으로 operator= 치료 :

문법적으로 적절한 버전이 : 나는 그것을 사용하는 것이 잘못이라고 생각하지만

template <class T> 
class Matrice 
{ 
    T m,n; 
public: 
    template <class V> 
    Matrice<V>& operator=(const Matrice<V> &); 
}; 

template <class T> 
template <class V> 
Matrice<V>& Matrice<T>::operator=(const Matrice<V> &M) 
{ 
    /*...*/ 
    return *this; 
} 

template <class V> 그; sematically 적절한 버전

template <class T> 
class Matrice 
{ 
    T m,n; 
public: 
    Matrice<T>& operator=(const Matrice<T> &); 
}; 

template <class T> 
Matrice<T>& Matrice<T>::operator=(const Matrice<T> &M) 
{ 
    /*...*/ 
    return *this; 
} 

설명 될 것이다 : 당신은 일반적으로Type<V> 이런 식으로 Type<T>에 할당 싶지 않아; 당신이해야한다면, 아마도 나쁜 디자인의 징후 일 것입니다.

+0

예를 들어, 다음과 같은 오류 메시지가 나타납니다. '오류 C2244 :'Matrice :: operator = ': 기존 선언에 함수 정의를 일치시킬 수 없습니다.' –

+0

@RevoltEr, 아니요. 내가 고칠 수있게 해줘. – Griwes

+0

Bojan Komazec의 솔루션이 더 단순 해 보입니다. 그 사람이나 당신을 사용해야합니까? 아니면 둘 다 똑같은 일을합니까? –

3

이 표준은

12.8 Copying and moving class objects [class.copy]

...

17 A user-declared copy assignment operator X::operator= is a non-static non-template member function of class X with exactly one parameter of type X, X&, const X&, volatile X& or const volatile X&.

친구 기능은 이러한 요구 사항을 충족하지 않습니다 말한다. 멤버 함수 여야합니다.

standardese에서

13.5.3 Assignment [over.ass]

An assignment operator shall be implemented by a non-static member function with exactly one parameter.

, 어떤을 떠나지 않아 "된다"


은의 표준에서 다른 견적을 추가 할 수 있도록하지 할당을 복사, 이것은 단지 "일반"과제라고 코멘트를 해결하기 위해 다른 일을하는 옵션. 당신이 친구operator=을 선언하지만, 클래스 멤버로 정의 된 첫 번째 예제 :

+0

연산자 =는 반드시 복사 할당 연산자 일 필요는 없습니다. – Fozi

+0

@Fozi, 어떤 경우에'X :: operator =()'가 복사 할당 연산자가 아닌가? 참고 : OP는'X :: operator =()'가 아니라'X :: = X();'... ... – Griwes

+1

그냥 과부하이기 때문에 = 연산자가 복사 중임을 의미하지는 않습니다. 위의 규칙을 따르지 않는 = 연산자의 오버로드는 복사 할당 연산자가 아니므로 컴파일러에서 생성 된 연산자를 생성 할 수 있습니다. – Fozi

2

당신은 친구회원 선언과 정의를 혼합. 두 번째 예에서는 operator=을 멤버로 선언했지만 멤버가 아닌 멤버로 정의하려고했습니다. 귀하는 operator= (this question를 참조 이유) 회원이어야하고 다음을 수행 할 수 있습니다

template <class T> 
class Matrice 
{ 
    T m,n; 
public: 
    Matrice<T>& operator=(Matrice<T> &); 
}; 

template <class T> 
Matrice<T>& Matrice<T>::operator=(Matrice<T> &M) 
{ 
    /*...*/ 
    return *this; 
} 
관련 문제