2012-03-31 76 views
3

我有一個方法接口和一個模擬該接口的類。該方法只有一個參數。只有當參數類型爲std::pair<Something, Something>時,纔會編譯失敗。我正在使用MSVC 2010,所以可能的問題是編譯器或STL實現的具體情況,除非這個問題與溼件相關,這是我最好的猜測。我必須失去一些明顯的東西。像納米探針一樣。Google中的std :: pair參數Mocked成員函數無法編譯

#include <gmock/gmock.h> 

class BorgInterface 
{ 
public: 
    typedef std::pair<int, long> MyBorg; // <--- MyBorg is problematic! 
    //typedef long MyBorg; // ..but this MyBorg complies 
    virtual void Assimilate(MyBorg borg_in_training) = 0; 
}; 

class MockBorg 
    : public BorgInterface 
{ 
public: 
    MOCK_METHOD1(Assimilate, void(BorgInterface::MyBorg borg_in_training)); 
}; 

/*TEST(MyBorgTestCase, BorgInterfaceTest) 
{ 
    using ::testing::_; 

    MockBorg funny_borg; 
    EXPECT_CALL(funny_borg, Assimilate(_)); 
    // ...etc. (irrelevant) 
}*/ 

實際的測試用例不必取消註釋錯誤以表明自己。

現在,我通過將std::pair<>包裝在struct中來解決此問題,但這不是最優的。

錯誤消息的長度是相當不幸的,但它可以幫助:

1>Build started 3/31/2012 4:02:43 PM. 
1>ClCompile: 
1> test_pair_parameter_mock.cpp 
1>c:\program files (x86)\microsoft visual studio 10.0\vc\include\tuple(127): 
    error C2664: 'std::pair<_Ty1,_Ty2>::pair(const std::pair<_Ty1,_Ty2> &)' 
     : cannot convert parameter 1 from 'int' to 'const std::pair<_Ty1,_Ty2> &' 
1>   with 
1>   [ 
1>    _Ty1=int, 
1>    _Ty2=long 
1>   ] 
1>   Reason: cannot convert from 'int' to 'const std::pair<_Ty1,_Ty2>' 
1>   with 
1>   [ 
1>    _Ty1=int, 
1>    _Ty2=long 
1>   ] 
1>   No constructor could take the source type, 
      or constructor overload resolution was ambiguous 
1>   c:\...\microsoft visual studio 10.0\vc\include\tuple(404) 
       : see reference to function template instantiation 
       'std::tr1::_Cons_node<_Car,_Cdr>::_Cons_node< 
        _Ty1&,_Ty2&,std::tr1::_Nil&,std::tr1::_Nil&, 
        std::tr1::_Nil&, 
        ............... 
        std::tr1::_Nil&, 
        std::tr1::_Nil&>(_Farg0,...,_Farg9)' being compiled 
1>   with 
1>   [ 
1>    _Car=BorgInterface::MyBorg, 
1>    _Cdr=std::tr1::_Tuple_type< 
        std::tr1::_Nil, 
        .............. 
        std::tr1::_Nil, 
        std::tr1::_Nil>::_Type, 
1>    _Ty1=int, 
1>    _Ty2=long, 
1>    _Farg0=int &, 
1>    _Farg1=long &, 
1>    _Farg2=std::tr1::_Nil &, 
1>    ....................... 
1>    _Farg9=std::tr1::_Nil & 
1>   ] 
1>   d:\...\gmock\include\gmock\gmock-generated-function-mockers.h(97) : 
       see reference to function template instantiation 
       'std::tr1::tuple<_Arg0>::tuple<int,long>(
        std::pair<_Ty1,_Ty2> &)' being compiled 
1>   with 
1>   [ 
1>    _Arg0=BorgInterface::MyBorg, 
1>    _Ty1=int, 
1>    _Ty2=long 
1>   ] 
1>   d:\...\gmock\include\gmock\gmock-generated-function-mockers.h(92) : 
       while compiling class template member function 
       'void testing::internal::FunctionMocker<Function>::Invoke(A1)' 
1>   with 
1>   [ 
1>    Function=void (BorgInterface::MyBorg), 
1>    A1=BorgInterface::MyBorg 
1>   ] 
1>   d:\..\myapp\src\tests\unit_tests\test_pair_parameter_mock.cpp(17) : 
       see reference to class template instantiation 
       'testing::internal::FunctionMocker<Function>' being compiled 
1>   with 
1>   [ 
1>    Function=void (BorgInterface::MyBorg) 
1>   ] 
1> 
1>Build FAILED. 

回答

1

看起來像一個編譯器的問題確實;這使用gcc 4.6編譯OK。

一個更簡單的解決辦法是通過指針傳遞MyBorg爲const:

virtual void Assimilate(const MyBorg *borg_in_training) = 0; 

,或者如果你樂於使用升壓,你可以用boost::tuple

typedef boost::tuple<int, long> MyBorg; 
+0

謝謝!我正在考慮提交錯誤報告谷歌模擬,但自從我開始使用它只是在最近,我認爲這更可能是我的錯。 – irobot 2012-03-31 20:43:11

1

取代std::pair如果你想有一個充分說明問題,有a bug report on this issue with a detailed discussion and investigation。它特定於VS2010,因爲它直接使用一對元組構造函數。相關說明是

試圖構造元組<對< T0,T1>>從一對< T0,T1>在所提到的第一個構造上述這又試圖 分配T0呼叫導致 配對< T0,T1>,產生錯誤。

該解決方案將是使從一對的構造元組只有 如果T0和T1從一對< T0,T1>從所述元組< T0給出作爲參數匹配T0和T1 ,T1>被構造。

這是VS2010的STL實現中的缺陷,並已在VS2012的STL實現中修復。

我成功地解決這個問題曾通過添加默認參數的函數簽名,以避免單一pair< T0, T1 >參數。這導致避免了有問題的構造函數。

相關問題