Tyler Brown
Tyler Brown

Reputation: 151

Adding elements of nested datatypes

#include <iostream>
#include <list>
#include <vector>
#include <fstream>

using namespace std;


class index_table
{
    public:
        index_table();
        void insert(string, int);
        void find();
    private:
        class entry
        {
            public:
                string word;
                vector<int> lineNum;    
        };
        vector<list<entry> > table;

};

index_table::index_table()
{
    table.resize(128);
}


void index_table::insert(string extrWord, int extrLineNum)
{
    int index = extrWord[0];
    list<entry>::iterator itor = table[index].begin();

    itor->word = extrWord;                                  //why doesnt this work???
    itor->lineNum.push_back(extrLineNum);

}

I am trying to find out why the above code is not working. I keep getting a seg fault or * glibc detected * ./a.out: munmap_chunk(): invalid pointer: 0x0000000001e46020 ***

I am creating a "table" that is a vector or lists that is an entry. Once each word and line number is passed to the insert function it needs to be added to the entry class...

Upvotes: 0

Views: 35

Answers (1)

frank.lin
frank.lin

Reputation: 1694

int index = extrWord[0];    
entry insEntry; 
insEntry.word = extrWord;                                     
insEntry.lineNum.push_back(extrLineNum);                               
table[index].push_back(insEntry)

Upvotes: 0

Related Questions