C++ segmentation fault from declaring a vector in a header

driver.cpp:

include <iostream>

include <fstream>

include <vector>

include "file_parser.h"

include "file_parse_exception.h"

using namespace std;

int main(int argc, char *argv[]) { if(argc != 2) { cout << "Error, you must supply the name of the file " << "to process at the command line." << endl; exit(1); } string filename = argv[1]; try { file_parser parser(filename); parser.read_file(); parser.print_file(); cout << "Token at 2,0 is: " << parser.get_token(2,0) << endl;
cout << "Token at 2,1 is: " << parser.get_token(2,1) << endl; cout << "Token at 2,2 is: " << parser.get_token(2,2) << endl; cout << "Token at 2,3 is: " << parser.get_token(2,3) << endl;

cout << "Token at 15,0 is: " << parser.get_token(15,0) << endl;    
cout << "Token at 15,1 is: " << parser.get_token(15,1) << endl;
cout << "Token at 15,2 is: " << parser.get_token(15,2) << endl;
cout << "Token at 15,3 is: " << parser.get_token(15,3) << endl;    

}
catch(file_parse_exception excpt) {
    cout << "**Sorry, error " << excpt.getMessage() << endl;
}

}

file_parser.h:

ifndef FILE_PARSER_H

define FILE_PARSER_H

include <string>

include <iostream>

include <vector>

include <fstream>

using namespace std;

class file_parser { public: // ctor, filename is the parameter. A driver program will read // the filename from the command line, and pass the filename to // the file_parser constructor. Filenames must not be hard-coded. file_parser(string);

// dtor
~file_parser();

// reads the source file, storing the information is some
// auxiliary data structure you define in the private section.
// Throws a file_parse_exception if an error occurs.
// if the source code file fails to conform to the above
// specification, this is an error condition.
void read_file();

// returns the token found at (row, column).  Rows and columns
// are zero based.  Returns the empty string "" if there is no
// token at that location. column refers to the four fields
// identified above.
string get_token(unsigned int, unsigned int);

// prints the source code file to stdout.  Should not repeat
// the exact formatting of the original, but uses tabs to align
// similar tokens in a column. The fields should match the
// order of token fields given above (label/opcode/operands/comments)
void print_file();

// returns the number of lines in the source code file
int size();

private: // your variables and private methods go here string file_name; string name; bool has_been_read; vector<string> contents; string hello; void parse_file(vector<string>); string comment; string label; };

endif

file_parser.cc:

include <iostream>

include <fstream>

include <vector>

include <cstdio>

include <string>

include "file_parser.h"

include "file_parse_exception.h"

using namespace std;

file_parser::file_parser(string file_name) { this->file_name = file_name; }

file_parser::~file_parser() { }

void file_parser::read_file() { /*ifstream infile(file_name.c_str()); string line; if(!infile) throw file_parse_exception::file_parse_exception("Error opening file"); while(!infile.eof()) { getline(infile,line); contents.push_back(line); } infile.close(); parse_file(contents); */ }

string file_parser::get_token(unsigned int, unsigned int) { return "Hello World"; }

void file_parser::print_file() {

}

int file_parser::size() { return 0; }

void file_parser::parse_file(vector<string> file) { /*string str; for(unsigned int i = 0; i < file.size(); i++) { string str = file[i]; string delimiters = " \t"; long last = str.find_first_not_of(delimiters,0); long first = str.find_first_of(delimiters,last); string token = " "; while(first != -1 || last != -1) { token = str.substr(last,first-last); tokens.push_back(token); last = str.find_first_not_of(delimiters,first); first = str.find_first_of(delimiters,last); }
tokens.clear(); //To reset vector tokens } */ }

file_parse_exception.h:

ifndef FILE_PARSE_EXCEPTION_H

define FILE_PARSE_EXCEPTION_H

include <string>

using namespace std;

class file_parse_exception {

public: file_parse_exception(string s) { message = s; }

file_parse_exception() {
    message = "An error has occurred";
}

string getMessage() {
    return message;
}

private: string message; };

endif

Makefile:

TORM = file_parser.o driver.o driver CC = g++ CCFLAGS = -g -O3 -Wall -Wpointer-arith -Wcast-qual -Wwrite-strings

driver: driver.o file_parser.o ${CC} ${CCFLAGS} -o driver driver.o file_parser.o

driver.o: driver.cpp ${CC} ${CCFLAGS} -c driver.cpp

file_parser.o: file_parser.cc file_parser.h file_parse_exception.h ${CC} ${CCFLAGS} -c file_parser.cc

clean: rm -f ${TORM}

source1.txt: .sample program for SIC/XE architecture . prog start 1000 .start of sample program lds #3 ldt #300 ldx #0 addlf lda alpha,x .loop control
add beta,x sta gamma,x
addr s,x compr x,t jlt addlp
. . storage allocation section
alpha resw 100 beta resw 100 gamma resw 100
end prog .end of sample program

/r/learnprogramming Thread