I am trying to write a parser for csv file using the boost.spirit library. I encounter the following compilation error. I am new in boost.spirit, so could someone identify the reasons?
The error message is:
Error C2664: 'bool boost::spirit::qi::rule::parse(Iterator &,const Iterator &,Context &,const Skipper &,Attribute &) const': cannot convert argument 1 from 'const char *' to 'std::_String_iterator>> &'
And my code is :
#pragma once
#define BOOST_SPIRIT_USE_PHOENIX_V3
#include<vector>
#include<string>
#include<memory>
#include<boost/iostreams/device/mapped_file.hpp> // for mmap
#include<boost/utility/string_ref.hpp>
#include<boost/spirit/include/qi.hpp>
#include<boost/spirit/include/qi_grammar.hpp>
#include<boost/spirit/include/qi_real.hpp>
#include<boost/spirit/include/phoenix.hpp>
#include<boost/spirit/include/qi_symbols.hpp>
typedef boost::string_ref CsvField;
typedef std::vector<CsvField> CsvLine;
typedef std::vector<CsvLine> CsvFile;
namespace qi = boost::spirit::qi;
template <typename T> struct CsvParser : qi::grammar<T, CsvFile()> {
CsvParser() : CsvParser::base_type(lines) {
using namespace qi;
using boost::phoenix::construct;
using boost::phoenix::size;
using boost::phoenix::begin;
using boost::spirit::qi::float_;
field = raw[*~char_(",\r\n")][_val = construct<CsvField>(begin(qi::_1), size(qi::_1))]; // semantic action
//field = qi::float_;
line = field % ',';
lines = line % eol;
}
// declare: line, field, fields
qi::rule<T, CsvFile()> lines;
qi::rule<T, CsvLine()> line;
qi::rule<T, CsvField()> field;
};
The code is really adopted from Simplest way to read a CSV file mapped to memory? , so I don't have any clue. I am using Microsoft Visual Studio 2015 and boost 1.16.0 .
This same error occurs if I replace typedef boost::string_ref CsvField
by typedef std::string
, or replace the parser for field by field = *(~char_(",\r\n"))
.
Also, the file I am parsing is really a standard csv file, so any suggestion of alternative parsing methods is welcome. The only catch is that the file has millions of rows, so the standard line-by-line parsing does not work for me.