C ++ tokenizer分隔符不编译

时间:2014-09-01 11:14:05

标签: c++ boost tokenize

我想用逗号分隔,并且我有以下用逗号分隔的行实例化的类。课程如下:

#include <sstream>
#include <stdio.h>
#include <iostream>
#include <ctime>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <vector>
#include <string>
#include <set>
#include <vector>
#include <boost/tokenizer.hpp>

class Packet {

    public:
        int packetEndDateTime;
        int creationTimeStamp;
        std::string mydatetime;
        std::string micses;
        std::string message_type;
        std::string teid;
        std::string teid_cp;
        std::string teid_data;
        std::string apn;
        std::string msisdn;
        std::string cause;
        std::string causeText;
        std::string responseDate;
        std::string allData;
        std::string fields[9];
        int fieldPos = 0;

        /*
        boost::char_separator<char> sep(",", "|", boost::keep_empty_tokens);
        typedef boost::tokenizer<boost::char_separator<char>> tokenizer;
         */
        typedef boost::tokenizer<boost::char_separator<char> > tokenizer;
        boost::char_separator<char> sep(",", "|", boost::keep_empty_tokens); // empty token policy

    Packet(){   }

    Packet(std::string inMessage){
        set_message(inMessage);
    }

    void set_message(std::string inMessage){

        allData = inMessage;

        tokenizer tokens(inMessage, sep);

        for ( tokenizer::iterator tok_iter = tokens.begin(); tok_iter != tokens.end(); ++tok_iter ){
            fields[fieldPos] = *tok_iter;
            fieldPos++;
        }

        mydatetime = fields[0];
        message_type = fields[1];
        teid = fields[2];
        teid_cp = fields[3];
        teid_data = fields[4];
        cause = fields[5];
        apn = fields[6];
        msisdn = fields[7];
    }

};

编译器回来了:

g++ -o ggsnGiParser welcome.cc -lboost_filesystem -lboost_program_options -lboost_system  -std=c++11
In file included from welcome.cc:49:0:
Packet.hpp:39:41: error: expected identifier before ','
Packet.hpp:39:41: error: expected ‘,’ or ‘...’ before ','
Packet.hpp: In member function ‘void Packet::set_message(std::string)’:
Packet.hpp:51:40: error: no matching function for call to ‘boost::tokenizer<boost::char_separator<char> >::tokenizer(std::string&, <unresolved overloaded function type>)’
Packet.hpp:51:40: note: candidates are:
In file included from Packet.hpp:12:0,
                 from welcome.cc:49:
/usr/include/boost/tokenizer.hpp:62:5: note: template<class Container> boost::tokenizer::tokenizer(const Container&, const TokenizerFunc&)
/usr/include/boost/tokenizer.hpp:62:5: note:   template argument deduction/substitution failed:
In file included from welcome.cc:49:0:
Packet.hpp:51:40: note:   cannot convert ‘((Packet*)this)->Packet::sep’ (type ‘<unresolved overloaded function type>’) to type ‘const boost::char_separator<char>&’
In file included from Packet.hpp:12:0,
                 from welcome.cc:49:
/usr/include/boost/tokenizer.hpp:58:5: note: template<class Container> boost::tokenizer::tokenizer(const Container&)
/usr/include/boost/tokenizer.hpp:58:5: note:   template argument deduction/substitution failed:
In file included from welcome.cc:49:0:
Packet.hpp:51:40: note:   candidate expects 1 argument, 2 provided
In file included from Packet.hpp:12:0,
                 from welcome.cc:49:
/usr/include/boost/tokenizer.hpp:53:5: note: boost::tokenizer<TokenizerFunc, Iterator, Type>::tokenizer(Iterator, Iterator, const TokenizerFunc&) [with TokenizerFunc = boost::char_separator<char>; Iterator = __gnu_cxx::__normal_iterator<const char*, std::basic_string<char> >; Type = std::basic_string<char>]
/usr/include/boost/tokenizer.hpp:53:5: note:   no known conversion for argument 1 from ‘std::string {aka std::basic_string<char>}’ to ‘__gnu_cxx::__normal_iterator<const char*, std::basic_string<char> >’
/usr/include/boost/tokenizer.hpp:32:9: note: boost::tokenizer<boost::char_separator<char> >::tokenizer(const boost::tokenizer<boost::char_separator<char> >&)
/usr/include/boost/tokenizer.hpp:32:9: note:   candidate expects 1 argument, 2 provided
/usr/include/boost/tokenizer.hpp:32:9: note: boost::tokenizer<boost::char_separator<char> >::tokenizer(boost::tokenizer<boost::char_separator<char> >&&)
/usr/include/boost/tokenizer.hpp:32:9: note:   candidate expects 1 argument, 2 provided

我真的不明白问题可能在哪里......

非常感谢任何帮助!

大卫

1 个答案:

答案 0 :(得分:3)

替换

boost::char_separator<char> sep(",", "|", boost::keep_empty_tokens); // empty token policy

boost::char_separator<char> sep = {",", "|", boost::keep_empty_tokens}; // empty token policy

在类声明中构造时,必须避免使用特定的()语法。

这可能会隐藏更多错误。