【问题标题】:Google mock does not compile with boost::variant of std::vectorGoogle mock 不能使用 std::vector 的 boost::variant 编译
【发布时间】:2012-05-31 08:49:22
【问题描述】:

我正在尝试为一些使用 boost::variant 的接口类创建 Google Mock 对象

#include <gtest/gtest.h>
#include <gmock/gmock.h>
#include <boost/variant.hpp>
#include <vector>

typedef std::vector<int> VectorOfInt;
typedef boost::variant<VectorOfInt> VariantOfVector;

class InterfaceClass
{
public:
     virtual ~InterfaceClass() {}
     virtual void SetSome( const VariantOfVector& ) = 0;
     virtual const VariantOfVector& GetSome() const = 0;
};

class MockInterfaceClass
{
public:
     MOCK_METHOD1( SetSome, void( const VariantOfVector& ) );
     MOCK_CONST_METHOD0( GetSome, const VariantOfVector&() );
};

当我用它编译时

g++ mytest.cpp -o mytest

我明白了

/usr/include/boost/variant/detail/variant_io.hpp:64: error: no match for 'operator >*)this )->boost::detail::variant::printer >>::out_

boost::variant 是否适用于 std::vector? boost::variant 似乎适用于我定义的任何类型,但 std:vector 除外。为什么?

增强版 - 1.45 g++ 版本 - 4.4.5

【问题讨论】:

  • 顺便说一下,整个编译清单是here

标签: boost googlemock


【解决方案1】:

似乎模拟尝试将运算符

【讨论】:

  • 感谢您的回答。可能它会解决我的问题。你知道我的例子中 operator
  • 好吧,也许像这样: template<class v> std::ostream&amp; operator &amp; rhs) { std: :copy(rhs.begin(), rhs.end(), std::ostream_iterator<typename std::vector>::value_type&gt;(out, ", "));退出; }</typename></class>
【解决方案2】:

正如 Igor R. 回答的那样,您需要像这样添加运算符

std::ostream& operator <<(std::ostream& out, VariantOfVector const& rhs)
{
    //Print or apply your visitor to **rhs**
    return out;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-02-01
    • 1970-01-01
    • 2012-08-21
    • 2014-05-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多