0

I have a simple code sample of a class that wraps boost::tokenizer.

MyTokenizer.h

#pragma once

#include <iostream>
#include <boost/tokenizer.hpp>

class MyTokenizer
{
public:
    typedef boost::tokenizer< boost::escaped_list_separator<char> > TokType;

    MyTokenizer(std::string);
    ~MyTokenizer() {};
    void printTok();

private:
    const TokType tok_;

};

MyTokenizer.cpp

#include "MyTokenizer.h"


MyTokenizer::MyTokenizer(std::string input) :
tok_(input)
{
    std::cout << "Created tokenizer from: " << input << std::endl;
    for (TokType::iterator it = tok_.begin(); it != tok_.end(); ++it){
        std::cout << *it << std::endl;
    }
}

void MyTokenizer::printTok(){
    std::cout << "printing tokens" << std::endl;
    for (TokType::iterator it = tok_.begin(); it != tok_.end(); ++it){
        std::cout << *it << std::endl;
    }
}

main.cpp

#include "MyTokenizer.h"

int main(void){
    std::string input("a,b,c");
    MyTokenizer tok(input);
    tok.printTok();
}

When I run this example it makes it through the constructor fine, printing the expected tokens in the loop but on the call to printTok() it gives this error

enter image description here

It seems like I cant create an iterator of MyTokenizer outside of the constructor.

Edit

I changed the printTok() method to be even simpler while still throwing the same error it now looks like this.

void MyTokenizer::printTok(){
    TokType::iterator it = tok_.begin();
}
rvabdn
  • 947
  • 6
  • 20

1 Answers1

1

Ok I fixed this myself. The problem was that the string I built my tokenizer from was being de-allocated at the end of the constructor. I fixed it by storing a copy of the input string in my class and building my tokenizer from this string.

rvabdn
  • 947
  • 6
  • 20