Test.h== 연산자 및 목록 : 삭제()
#ifndef TEST_H
#define TEST_H
#include <memory>
template <class Type>
bool operator==(const std::weak_ptr<Type>& wp1, const std::weak_ptr<Type>& wp2)
{
std::shared_ptr<Type> sp1;
if(!wp1.expired())
sp1 = wp1.lock();
std::shared_ptr<Type> sp2;
if(!wp2.expired())
sp2 = wp2.lock();
return sp1 == sp2;
}
#endif
Test.cpp에
#include "Test.h"
#include <list>
int main()
{
typedef std::list< std::weak_ptr<int> > intList;
std::shared_ptr<int> sp(new int(5));
std::weak_ptr<int> wp(sp);
intList myList;
myList.push_back(wp);
myList.remove(wp); //Problem
}
프로그램은 (인해 myList.remove로 컴파일되지 않음) :
1> c : \ program files (x86) \ microsoft visual studio 10.0 \ vc \ include \ list (1194) : 오류 C2678 : 바이너리 '==': 왼쪽 연산자'std :: tr1 :: weak_ptr < _Ty>'을 사용하는 연산자가 없습니다 (또는 허용되지 않습니다 변환) 1 1>
> [1> _Ty = INT 1>]
그러나 당신이이 Test.h에 정의 된 다음 볼 수 있습니다
bool operator==(const std::weak_ptr<Type>& wp1, const std::weak_ptr<Type>& wp2)
문제가 무엇입니까?
잘 모르겠지만 const 참조로 부울 연산자 ==를 정의 할 수 있습니까? – CharlesB
와우, 나는 원래 그런 식으로 그것을 다시 잊어 버렸습니다. const 참조와 같은 문제. – user987280