I am working on a program that takes in a SIC/XE assembler program (in the form of a tab-delimited .txt file) and generates a file with the opcodes for each line. All the weight is on me right now, as I am the one who must start this. I was torn between trying to do it all with one class (which could get messy!!) or just have multiple classes (like I was trying to do in my first post), which would make for an easy teachable moment. I am leaning toward the second approach, but before making the multiple respective .cpp and .h files, I was going to try it out on the class that is already in my main.cpp file. The way it would have to work is that the constructor would accept the line of code (provided that it doesn't start with '.', which is a comment). I have tried:
1 2 3 4 5 6 7 8 9
ifstream myfile (directory.c_str());
//This vector is causing problems...
vector <assemblytoopcode> avector;
while (myfile.is_open())
{
getline(myfile, line);
avector.push_back(assemblytoopcode(directory));
} //end while
myfile.close();
where directory (the variable I passed to the constructor by trying the first approach) is going to be replaced with the line itself. For some reason, when I run my whole program, it runs but hangs. (It was working fine without this code snippet.) What is happening?
Would it be because the vector is empty when I am declaring it? I know that with fixed-length dynamic arrays, the values are just set to NULL, or whatever random value the computer thinks of, causing you to need something like:
1 2 3 4 5 6
myclass * classarray = new (nothrow) myclass[x];
for (int y = 0; y < x; y++)
{
classarray[y] = myclass(constructorvalue);
//functions in class being called
} //end for
EDIT: Don't tell me that I would have already read (and counted) the lines and reserved (at most) that many elements in the vector.
I ditched this approach. It turns out you would have to reserve the spots for the constructors. This is because, in the fixed-length dynamic array approach, the elements are already there, set to whatever the heck the program is thinking at the time, as long as it conforms to the data type of the array. This results in (correct me if I am wrong) calling the default constructor. Now, when you perform the assignment operation, you are setting each constructor equal to the parameterized constructor, thus calling it. On the other hand, how can you call the default constructor with literally nothing in the vector? You can't, and this is why you must reserve space.