You're not failing "because of pointers", but because of the confusion you have dispersed almost everywhere.
In you code the role of the various classes is not clear. As it is not clear if derivation is used for runtime-specialization or extension.
class MainNode
{
public:
vector<mainnode*> *elements;
MainNode () { elements = NULL;}
virtual void writeCode(fstream &outfile) = 0;
};
What the hell is
mainnode
used in vector? Why is the vector instantiated through a pointer?
You write
elements->at(index)
But also write
(*node).writeCode(outfile)
(why not
node->writecode(outfile)
? Try to be consistent!)
You do
dynamic_cast<scxmlstate*>(parent)->id
But if you dynamic_cast it means you want to check if the runtime type of a node is ans sxcmlstate (What the hell is it? I see a ScxmlState, but not a scxmlstate). But dynamic cast may fail to NULL, but you directly address
id
without any check (but if you use dynamic_cast to check the type, what is the purpose to have an id ??)
The impression I have is that you are coding bottom-up, pushing statements around and attempting to let everything go at its place automatically.
I suggest you not to continue that way (you will sooner or later with a big mess that even if it may work, you'll never know WHY it works, and the day it will stop working you will have no clue about where to start for.
So throw away this mess, and start with pencil and paper, and try to fiugure out what the role of our classes is, how they relates to each other, how those relations should translated in aggregation mechanisms (embedding of a valie, pointer to dynamically allocated object you have to destroy, pointer to something owned by somebody else ...), then start writing classes, members, and proper constructor and destructors (with the sane policy that what is allocated is also deallocated, and a proper ownership if object is defined).
Then write a test program that checks that all the relations properly works and nothing had been forgot.
After that start coding input and output functions that creates the data structure based on proper input and write proper output.