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 \包括\列表(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)
什么问题?
不知道,但你可以尝试定义布尔运算符==常量引用? – CharlesB 2012-04-15 21:53:15
哎呀,我原来是这么想的,忘了把它改回来。与const引用同样的问题。 – user987280 2012-04-15 21:56:47