Quote:
Originally Posted by olf
A few more problems have arised and I hope that it is ok if I ask more questions.
Absolutly not, we have a strict quota of 1 question per poster per week...
oh hang on that's not right, we don't have any quota at all :p
OK sarcasm aside please ask as many questions as you like generally we will attempt to answer them all.
Quote:
Originally Posted by olf
I have now read and tokenized the strings. What I want to do know is to create numerous structures that hold the information that I have read.
The thing that I am working on involves trains in a train network. So I want to store information about the trains in one type of struct and information about the tracks in another type.
So basically I want two separate lists of structures.
Am I right in assuming that I will have to pass along these lists throughout the functions? Ie create them in main, pass them to readFile and then pass them to fillData where each train/node is assigned values.
Well sort of, however at this point I am not sure you have a good class hierarchy and you need to get that right first.
You have an IO class, this reads the file and creates trains. This is not good, trains are nothing to do with file IO, additionally you have your train and node lists in main. You have trains, nodes (not clear to me exactly what this represents), junctions and segments (track segments?). What you don't have is a Network class.
In my mind the network would be the top level, every train, junction, node and segement must belong to a rail network and this should be represented. The network would then contain the lists of trains etc and in main you would have a single declaration of a single network instance.
The network would contin the functions to load the data from a file but because it also contains the lists of trains etc there would then be no need to pass these around so you could write straight to them.
I would re-write functions like void Io::createTrain(...) as a constructor of the train struct (I might make a class to).
What you are aiming for is each class/structure accessing its own data and a reduction of class/structure data access externally to the clss/structure.
Remember the only difference between a class and a struct is that a class members have a default access of private and a struct memebers have a default access of public.
The IO class is not helpful because it does not excapsulate anything real.
Quote:
Originally Posted by olf
There are several things that are both wrong and that bothers me. Lets start with the temp-array in fillData, it feels horribly wrong to declare it with a static size. I think I should be able to read the length of line and declare it with that size but I just cant get that to work.
It feels horrible because it is horrible, and it is also asking for trouble. Dynamically allocating the data is not hard
-
void Io::fillData(string& line){
-
char *temp = new char[line.length()+1];// +1 for terminator
-
-
if (temp != NULL) // Just check the memory allocation worked
-
{
-
// Copy line totemp and then terminate the string
-
line.copy(temp, line.length());
-
temp[line.length()] = '\0';
-
-
// <snipped tokenising code>
-
-
delete[] temp;
-
}
-
else
-
{
-
// Memory allocation failure handling code
-
}
-
}
-
Quote:
Originally Posted by olf
The same problem arises with the trainlist and the nodelist in main.
Look up the C++ standard containers vector, deque, list, map, set. These handle dynamically sized lists for you and are well worth learning because once you know them you will use them all the time. I would have thought either list or map would suit this application.
Quote:
Originally Posted by olf
The other problem is how I pass the two lists in main correctly through to the other functions. Since I want to use the data later on I thought I would take them as references in readFile and fillData, but that turns out to be impossible as "arrays of references are illegal".
As I explained above a "Network" class contining the train etc lists will solve this as its member functions will have access to its member data so there will be no need to pass the data around.