2016-04-14 15 views
1

boost.spiritライブラリを使用してcsvファイルのパーサーを作成しようとしています。次のコンパイルエラー私はboost.spiritで新しいですので、誰かがその理由を特定することができboost.spiritコンパイルエラー: "const char *"から "std :: _ St​​ring_iterator <std :: _ St​​ring_val>に変換できません。

エラーメッセージは次のとおりです。?

エラーC2664:「後押しBOOL ::精神::チー::ルール:: parse(反復子&、constイテレータ&、文脈&、constスキッパー&、属性&)cons T ':から引数1を変換することはできません ':コードは本当にSimplest way to read a CSV file mapped to memory?から採用されている

#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; 
}; 

ので、あるのstd :: _ St​​ring_iterator >> & '

そして、私のコード' から' のconstのchar *私は何の手がかりも持っていません。私はMicrosoft Visual Studio 2015を使用し、1.16.0を向上させています。

このエラーは、typedef boost::string_ref CsvFieldtypedef std::stringに置き換えるか、フィールドのパーサーをfield = *(~char_(",\r\n"))に置き換えた場合に発生します。

また、私が解析しているファイルは実際には標準のCSVファイルなので、代替の解析方法の提案は歓迎します。唯一の捉え方は、ファイルに何百万もの行があることです。そのため、標準の行単位の解析は私のためには機能しません。

答えて

0

関連するコードを表示していません。あなたが持っているのはテンプレートクラスですが、インスタンシエーションがうまく構成されているかどうかは、インスタンス化するものによって決まります。

今は、イテレータタイプとしてstd::string::const_iteratorでインスタンス化しようとしていると思います。これは面白いw.r.tです。メモリマッピングとstring_refの記述(これは、すべてゼロコピーを実行することを意味します)。

それでも、問題はその後raw[]はあなたがstring_ref(別名CsvField)コンストラクタの最初の引数としてstd::string::const_iteratorを渡している意味、ソースイテレータ型のiterator_rangeを公開するということです。それは動作しません。この修正で

field = raw[*~char_(",\r\n")][_val = construct<CsvField>(&*begin(qi::_1), size(qi::_1))]; // semantic action 

あなたはフェニックスの俳優でstd::addressofをラップし、operator&の代わりにそれを使用する必要があります本当にいいとします。私はそれを読者のための練習として残しておきます。

Live On Coliru

#define BOOST_SPIRIT_USE_PHOENIX_V3 
#include<boost/utility/string_ref.hpp> 
#include<boost/spirit/include/qi.hpp> 
#include<boost/spirit/include/phoenix.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; 
}; 

int main() 
{ 
    using It = std::string::const_iterator; 
    CsvParser<It> p; 

    std::string const input = R"([section1] 
key1=value1 
key2=value2 
[section2] 
key3=value3 
key4=value4 
)"; 

    CsvFile parsed; 
    auto f = input.begin(), l = input.end(); 
    bool ok = parse(f, l, p, parsed); 

    if (ok) { 
     std::cout << "Parsed: " << parsed.size() << " stuffs\n"; 
    } else { 
     std::cout << "Parse failed\n"; 
    } 

    if (f != l) 
     std::cout << "Remaining input: '" << std::string(f, l) << "'\n"; 
} 

印刷:

Parsed: 7 stuffs 
関連する問題