6

次のGoogleモック宣言を使用しているときにコンパイルエラーが発生します。

EXPECT_CALL(some_object, someFunction(1,An<AStructIDefined>()))
    .Times(2);

エラーは次のとおりです。

1>ClCompile:
1>  TestMyClass.cpp
1>TestMyClass.cpp(189): error C2664: 'mynamespace::MockMyClassClient::gmock_someFunction' : cannot convert parameter 2 from 'testing::Matcher<T>' to 'const testing::Matcher<T> &'
1>          with
1>          [
1>              T=mynamespace::AStructIDefined
1>          ]
1>          and
1>          [
1>              T=const mynamespace::AStructIDefined &
1>          ]
1>          Reason: cannot convert from 'testing::Matcher<T>' to 'const testing::Matcher<T>'
1>          with
1>          [
1>              T=mynamespace::AStructIDefined
1>          ]
1>          and
1>          [
1>              T=const mynamespace::AStructIDefined &
1>          ]
1>          No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called

私は何が間違っているのですか?


更新:

VS2010を使用しています。

someFunctionの宣言は次のとおりです。

virtual void someFunction( long long ll, const AStructIDefined& a_struct);

An()は、次の定義を持つGoogleMockワイルドカードマッチャーです。

// Creates a matcher that matches any value of the given type T.
template <typename T>
inline Matcher<T> An() { return A<T>(); }

構造体の簡略化された代表的なバージョンは次のとおりです。

namespace mynamespace {

class ABaseCLass
{
public:
    virtual ~ABaseCLass(){};
    virtual bool isValid() const = 0;
};

struct AStructIDefined : public ABaseCLass
{
public:
    OrderStatusReport(SomeEnum1 e_, int i_, double d_);

    SomeEnum1 e;
    int i;
    double d;

    const std::string toString() const;
    bool isSane() const;
    bool operator== (const SomeEnum1& ref_) const;
    double getD() const;
    int getI() const;
    bool isCondition() const;
};

} // namespace mynamespace
4

1 に答える 1

4

解決策は、宣言を次のように変更することでした。

EXPECT_CALL(some_object, someFunction(1,An<AStructIDefined>()))
    .Times(2);

EXPECT_CALL(some_object, someFunction(1,An<const AStructIDefined &>()))
    .Times(2);

C ++は暗黙的に関数パラメーターをキャストconstおよび参照&しますが、google mockの宣言では、関数にパラメーターとして送信された型ではなく、関数のシグネチャに表示される型が必要なようです。

于 2011-02-20T12:37:48.500 に答える