if (argc > 0) {
interpreter = new Interpreter(argv[1]);
This is not correct, if argc == 1
then argv[1]
is out of bounds, it should be
if (argc > 1) {
interpreter = new Interpreter(argv[1]);
As for the rest of the question, I would write the constructor like this:
Interpreter(const std::string location) : file(location) { }
(In C++11 you can construct an fstream
from a std::string
, if that doesn't work with your compiler then use location.c_str()
as you had previously:
Interpreter(const std::string location) : file(location.c_str()) { }
I would write your main
function like this:
int main(int argc, char** argv)
{
std::string file;
// check if arguments are provided
if (argc > 1) {
file = argv[1];
} else {
// for now just use the debug script
error("No input files, using default script (debug)");
file = "test.m";
}
Interpreter interpreter(file);
interpreter.read();
}
This has no new
and delete
and is simpler and clearer.