3

我收到以下编译器错误:

/usr/include/boost/variant/variant.hpp:832:32: error: no match for call to '(const StartsWith) (bool&)'</p>

对于以下代码。有人知道为什么吗?

#include "boost/variant/variant.hpp"
#include "boost/variant/apply_visitor.hpp"

using namespace std;
using namespace boost;

typedef variant<bool, int, string, const char*> MyVariant;

class StartsWith
    : public boost::static_visitor<bool>
{
public:
    string mPrefix;
    bool operator()(string &other) const
    {
        return other.compare(0, mPrefix.length(), mPrefix);
    }
    StartsWith(string const& prefix):mPrefix(prefix){}
};

int main(int argc, char **argv) 
{
    MyVariant s1 = "hello world!";
    apply_visitor(StartsWith("hel"), s1); // << compiler error
    return 0;
}
4

1 回答 1

4

您必须为在 中声明的每种类型提供运算符MyVariant

于 2012-11-07T07:43:20.800 回答