So, I have two classes defined, gramNode and molecule.
I have linked lists created, the nodes of which are of the class gramNode. The class molecule has some data, and it includes a pointer to a gramNode. So each object of the class molecule has a linked list of gramNodes attached to it.
Here are my class definitions.
Code:
class gramNode
{
public:
gramNode(){count = 0, gram = " ", next = NULL;}
gramNode(string subString);
void setGram(string subString);
int getCount()const{return count;};
string getGram()const{return gram;};
int writeList(ofstream *osptr, int numGram);
private:
string gram;
int nValue, count;
gramNode* next;
};
class molecule
{
public:
molecule(){SMILES = " ", entropy = 0, head;}
molecule(string subString){SMILES = subString, entropy = 0, head;};
void setSMILES(string subString){SMILES = subString;};
void setGram(string gram){head->setGram(gram);};
void setPtr(){head = new gramNode;};
string getSMILES()const{return SMILES;};
void writeGrams(ofstream *osptr);
private:
string SMILES;
double entropy;
gramNode* head;
};
each node has a string and an integer stored in it. I want to run down the linked list and output the string and number for each list. I had to do this earlier in the program, so I write this function. It's called from within a gramNode, and it does exactly what I described. I use this function elsewhere in my program and it works perfectly.
Code:
int gramNode::writeList(ofstream *osptr, int numGram)
{
gramNode* seek = this;
while (seek!= NULL)
{
*osptr << numGram << '\t' << seek->gram << '\t' << seek->count << endl;
numGram++;
seek = seek->next;
}
return numGram;
}
My problem is, when I want to output the list for a specific molecule. I thought the problem would be easy enough, just write a function that will go to the 1st node attached to the molecule and evoke the writeList function. So, I wrote this.
Code:
void molecule::writeGrams(ofstream *osptr)
{
head->writeList(osptr, 1);
}
the 1 is something left over from when I used the function earlier in the program. Sometimes it's important to keep track of that number, other times it isn't. So in this case, I just set it to 1 and let it run.
Code compiles and builds fine, and then the program crashes when I run it.
Any help would be greatly appreciated, thanks guys.