| re: how to store data in a different class object?
If I understand your problem correctly , I would write the
function
Document *addDocument(iostream& inStream, string documentId);
as follows: Document *Repository::addDocument(iostream& inStream, string documentId)
{
char line[250];
inStream.getline(line,250);
char * ptrToToken = NULL;
ptrToToken = strtok (line," ,.-");//assumes words are separated
//by spaces,comma,dot or hyphen
Sentence * ptrToSent = new Sentence();
Document * ptrToDoc = new Document(documentId);
ptrToSent -> setDocument(ptrToDoc);
ptrToDoc ->setRepository(this);
while (ptrToToken != NULL)
{
Word * ptrToWord = new Word(ptrToToken);
ptrWord -> setSentence(ptrToSent);
ptrToScent -> addWord(ptrToWord)
ptrToToken = strtok (NULL, " ,.-");
}
return ptrToDoc;
}
Before writing the above function , I assume there is a
data member and member function in Sentence class like the following :
Data member:
WordCollection wordColl;
member function:
Sentence::addWord(Word * ptrToWord)
{
wordColl.push_back(ptrToWord);
}
|