Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
176 views
in Technique[技术] by (71.8m points)

c++ - Example parsing error

I'm trying to parse an example of boost spirit (2.5.2) following the example. My code is the following

#include <boostspirithomeqi.hpp>
#include <iostream>
#include <string>
#include <utility>

int main()
{

  // Parsing two numbers
  std::string input("1.0 2.0");
  std::pair<double, double> p;

  boost::spirit::qi::phrase_parse(
    input.begin(),
    input.end(),
    boost::spirit::qi::double_ >> boost::spirit::qi::double_ , // Parse grammar
    boost::spirit::qi::space,
    p
  );

  return 0;
}

It's almost equal to the example found here, but when I compile it with Visual studio 2010 (32 bit, debug) I obtain the following error:

error C2440: 'static_cast': unable to convert from 'const double' to 'std::pair<_Ty1,_Ty2>'

(the error can be slighty different, I've translated it from italian)

What I'm doing wrong and how can I compile successfully the example?

Thanks in advance for your replies.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You are missing an include:

#include <boost/fusion/adapted/std_pair.hpp>

It defines the attribute assignment rules to make Fusion sequences (vector2<>) assignable to std::pair.

See the code live: liveworkspace.org

#include <boost/spirit/include/qi.hpp>
#include <boost/fusion/adapted/std_pair.hpp>
#include <iostream>
#include <string>
#include <utility>

int main()
{
    // Parsing two numbers
    std::string input("1.2 3.4");
    std::pair<double, double> p;

    namespace qi = boost::spirit::qi;

    qi::phrase_parse(
            input.begin(), 
            input.end(),
            qi::double_ >> qi::double_ , // Parse grammar
            qi::space, p);

    std::cout << "Lo:     " << p.first << "
"
              << "Behold: " << p.second << "
";
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...