C++ 具有弱ptr的河马
只是遇到了一个编译错误,代码试图模拟一个以C++ 具有弱ptr的河马,c++,comparison,operator-keyword,weak-ptr,hippomocks,C++,Comparison,Operator Keyword,Weak Ptr,Hippomocks,只是遇到了一个编译错误,代码试图模拟一个以std::weak_ptr为参数的方法HippoMocks有一个代码,在调用方法时与进行比较,该方法不会被编译。我将感谢任何帮助 代码如下: struct SomeClass { SomeClass(){}; void someMethod(std::weak_ptr<int> d) {} }; bool operator == (const std::weak_ptr<int> lhs, const std::we
std::weak_ptr
为参数的方法HippoMocks
有一个代码,在调用方法时与
进行比较,该方法不会被编译。我将感谢任何帮助
代码如下:
struct SomeClass {
SomeClass(){};
void someMethod(std::weak_ptr<int> d) {}
};
bool operator == (const std::weak_ptr<int> lhs, const std::weak_ptr<int> rhs) {
return *(lhs.lock()) == *(rhs.lock());
}
BOOST_AUTO_TEST_CASE(test_weak_ptr)
{
SomeClass obj;
std::weak_ptr<int> data = std::make_shared<int>(new int(15));
HippoMocks::MockRepository mocks;
mocks.ExpectCall(&obj, SomeClass::someMethod).With(data);
}
struct SomeClass{
SomeClass(){};
void-someMethod(std::weak_ptr d){
};
布尔运算符==(常数标准::弱ptr lhs,常数标准::弱ptr rhs){
return*(lhs.lock())==*(rhs.lock());
}
增压自动测试用例(测试弱ptr)
{
类obj;
std::weak_ptr data=std::make_shared(新int(15));
河马:mocks;
ExpectCall(&obj,SomeClass::someMethod).With(数据);
}
MS VS 2010编译器不喜欢最后一行,并抱怨:
d:\hippomocks\hippomocks.h(398): error C2678: binary '==' : no operator found which takes a left-hand operand of type 'const std::tr1::weak_ptr<_Ty>' (or there is no acceptable conversion)
with
[
_Ty=int
]
c:\program files (x86)\microsoft sdks\windows\v7.0a\include\guiddef.h(192): could be 'int HippoMocks::operator ==(const HippoMocks::GUID &,const HippoMocks::GUID &)'
while trying to match the argument list '(const std::tr1::weak_ptr<_Ty>, const std::tr1::weak_ptr<_Ty>)'
with
[
_Ty=int
]
d:\hippomocks\hippomocks.h(397) : while compiling class template member function 'bool HippoMocks::comparer<T>::compare(const std::tr1::weak_ptr<_Ty>,const std::tr1::weak_ptr<_Ty>)'
with
[
T=std::tr1::weak_ptr<int>,
_Ty=int
]
d:\hippomocks\hippomocks.h(650) : see reference to class template instantiation 'HippoMocks::comparer<T>' being compiled
with
[
T=std::tr1::weak_ptr<int>
]
d:\hippomocks\hippomocks.h(649) : while compiling class template member function 'bool HippoMocks::copy_tuple<A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,CA,CB,CC,CD,CE,CF,CG,CH,CI,CJ,CK,CL,CM,CN,CO,CP>::operator ==(const HippoMocks::ref_tuple<A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P> &)'
with
[
A=std::tr1::weak_ptr<int>,
B=HippoMocks::NullType,
C=HippoMocks::NullType,
D=HippoMocks::NullType,
E=HippoMocks::NullType,
F=HippoMocks::NullType,
G=HippoMocks::NullType,
H=HippoMocks::NullType,
I=HippoMocks::NullType,
J=HippoMocks::NullType,
K=HippoMocks::NullType,
L=HippoMocks::NullType,
M=HippoMocks::NullType,
N=HippoMocks::NullType,
O=HippoMocks::NullType,
P=HippoMocks::NullType,
CA=const std::tr1::weak_ptr<int> &,
CB=HippoMocks::NullType,
CC=HippoMocks::NullType,
CD=HippoMocks::NullType,
CE=HippoMocks::NullType,
CF=HippoMocks::NullType,
CG=HippoMocks::NullType,
CH=HippoMocks::NullType,
CI=HippoMocks::NullType,
CJ=HippoMocks::NullType,
CK=HippoMocks::NullType,
CL=HippoMocks::NullType,
CM=HippoMocks::NullType,
CN=HippoMocks::NullType,
CO=HippoMocks::NullType,
CP=HippoMocks::NullType
]
d:\hippomocks\hippomocks.h(2985) : see reference to class template instantiation 'HippoMocks::copy_tuple<A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,CA,CB,CC,CD,CE,CF,CG,CH,CI,CJ,CK,CL,CM,CN,CO,CP>' being compiled
with
[
A=std::tr1::weak_ptr<int>,
B=HippoMocks::NullType,
C=HippoMocks::NullType,
D=HippoMocks::NullType,
E=HippoMocks::NullType,
F=HippoMocks::NullType,
G=HippoMocks::NullType,
H=HippoMocks::NullType,
I=HippoMocks::NullType,
J=HippoMocks::NullType,
K=HippoMocks::NullType,
L=HippoMocks::NullType,
M=HippoMocks::NullType,
N=HippoMocks::NullType,
O=HippoMocks::NullType,
P=HippoMocks::NullType,
CA=const std::tr1::weak_ptr<int> &,
CB=HippoMocks::NullType,
CC=HippoMocks::NullType,
CD=HippoMocks::NullType,
CE=HippoMocks::NullType,
CF=HippoMocks::NullType,
CG=HippoMocks::NullType,
CH=HippoMocks::NullType,
CI=HippoMocks::NullType,
CJ=HippoMocks::NullType,
CK=HippoMocks::NullType,
CL=HippoMocks::NullType,
CM=HippoMocks::NullType,
CN=HippoMocks::NullType,
CO=HippoMocks::NullType,
CP=HippoMocks::NullType
]
d:\unittests\test.cpp(31) : see reference to function template instantiation 'HippoMocks::TCall<Y,A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P> &HippoMocks::TCall<Y,A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P>::With<std::tr1::weak_ptr<_Ty>>(const CA &)' being compiled
with
[
Y=void,
A=std::tr1::weak_ptr<int>,
B=HippoMocks::NullType,
C=HippoMocks::NullType,
D=HippoMocks::NullType,
E=HippoMocks::NullType,
F=HippoMocks::NullType,
G=HippoMocks::NullType,
H=HippoMocks::NullType,
I=HippoMocks::NullType,
J=HippoMocks::NullType,
K=HippoMocks::NullType,
L=HippoMocks::NullType,
M=HippoMocks::NullType,
N=HippoMocks::NullType,
O=HippoMocks::NullType,
P=HippoMocks::NullType,
_Ty=int,
CA=std::tr1::weak_ptr<int>
]
d:\hippomocks\hippomocks.h(398):错误C2678:二进制“==”:未找到接受“const std::tr1::弱”类型的左操作数的运算符(或没有可接受的转换)
具有
[
_Ty=int
]
c:\ProgramFiles(x86)\microsoft SDK\windows\v7.0a\include\guiddef.h(192):可以是“int HippoMocks::operator=(const HippoMocks::GUID&,const HippoMocks::GUID&)”
尝试匹配参数列表时“(const std::tr1::weak_ptr,const std::tr1::weak_ptr)”
具有
[
_Ty=int
]
d:\hippomocks\hippomocks.h(397):编译类模板成员函数“bool hippomocks::comparer::compare(const std::tr1::weak_ptr,const std::tr1::weak_ptr)”时
具有
[
T=std::tr1::弱,
_Ty=int
]
d:\hippomocks\hippomocks.h(650):请参阅对正在编译的类模板实例化'hippomocks::comparer'的引用
具有
[
T=std::tr1::弱
]
d:\hippomocks\hippomocks.h(649):编译类模板成员函数“bool hippomocks::copy\u tuple::operator==(const hippomocks::ref\u tuple&)”时
具有
[
A=std::tr1::弱ptr,
B=河马::NullType,
C=河马::NullType,
D=河马::NullType,
E=河马::NullType,
F=河马::NullType,
G=河马::NullType,
H=河马::NullType,
I=HippoMocks::NullType,
J=河马::NullType,
K=河马::NullType,
L=HippoMocks::NullType,
M=河马::NullType,
N=河马::NullType,
O=河马::NullType,
P=河马::NullType,
CA=const std::tr1::弱\u ptr&,
CB=河马::NullType,
CC=河马::NullType,
CD=HippoMocks::NullType,
CE=HippoMocks::NullType,
CF=HippoMocks::NullType,
CG=河马::NullType,
CH=河马::NullType,
CI=河马::NullType,
CJ=河马::NullType,
CK=河马::NullType,
CL=河马::NullType,
CM=河马::NullType,
CN=HippoMocks::NullType,
CO=河马::NullType,
CP=HippoMocks::NullType
]
d:\hippomocks\hippomocks.h(2985):请参阅对正在编译的类模板实例化'hippomocks::copy\u tuple'的引用
具有
[
A=std::tr1::弱ptr,
B=河马::NullType,
C=河马::NullType,
D=河马::NullType,
E=河马::NullType,
F=河马::NullType,
G=河马::NullType,
H=河马::NullType,
I=HippoMocks::NullType,
J=河马::NullType,
K=河马::NullType,
L=HippoMocks::NullType,
M=河马::NullType,
N=河马::NullType,
O=河马::NullType,
P=河马::NullType,
CA=const std::tr1::弱\u ptr&,
CB=河马::NullType,
CC=河马::NullType,
CD=HippoMocks::NullType,
CE=HippoMocks::NullType,
CF=HippoMocks::NullType,
CG=河马::NullType,
CH=河马::NullType,
CI=河马::NullType,
CJ=河马::NullType,
CK=河马::NullType,
CL=河马::NullType,
CM=河马::NullType,
CN=HippoMocks::NullType,
CO=河马::NullType,
CP=HippoMocks::NullType
]
d:\unittests\test.cpp(31):请参阅对正在编译的函数模板实例化“HippoMocks::TCall&HippoMocks::TCall::With(const CA&)”的引用
具有
[
Y=无效,
A=std::tr1::弱ptr,
B=河马::NullType,
C=河马::NullType,
D=河马::NullType,
E=河马::NullType,
F=河马::NullType,
G=河马::NullType,
H=河马::NullType,
I=HippoMocks::NullType,
J=河马::NullType,
K=河马::NullType,
L=HippoMocks::NullType,
M=河马::NullType,
N=河马::NullType,
O=河马::NullType,
P=河马::NullType,
_Ty=int,
CA=std::tr1::弱ptr
]
这是一种依赖于参数的查找方法,它会把你弄糊涂。它只在参数的名称空间中查找,即“HippoMocks”、“std”和“std”。这意味着它甚至不会考虑全局运算符==.<
解决方案: