You should initialize the pointer answer
first, like :
ClassSpecList *answer = new ClassSpecList;
then you can add thisAnswer
into this list.
Question
I have a data structure defined up here called this:
typedef list <classSpec*> ClassSpecList;
I'm trying to add stuff into the list here based on functions that return certain values of that match the same data type. In one function, I have a list pointer object defined here and I have another statement that calls a function.
ClassSpecList *answer = 0;
classSpec *thisanswer = parseClass(br);
Basically I'm trying to add the results of what thisanswer
returns into my main ClassSpecList
. Problem is, when I try
answer->push_back(new classSpec (*thisanswer));
It compiles but I get a seg fault
When I try somethign else like:
answer->insert(ClassSpecList.begin(), *thisanswer);
I keep getting primary expression errors and I do not know why. I even tried it with other list made without typedef
and I still get those.
Thank you.
Solution
You should initialize the pointer answer
first, like :
ClassSpecList *answer = new ClassSpecList;
then you can add thisAnswer
into this list.
OTHER TIPS
This should work:
ClassSpecList *answer = new ClassSpecList;
answer->push_back(thisAnswer);
as should this, which is usually recommended:
ClassSpecList answer;
answer.push_back(thisAnswer);
If possible, parseClass
shouldn't return a pointer, and you should use typedef list <classSpec> ClassSpecList;
.