I have created a linked list class VW with a function Build to add nodes to the list. However, when I run the code to build my linked list, the Build function throws an "access violation" error. The error is thrown by the call tmp->NextObj() in the Build function.
If the object of class VW is newly created and the Build is called for the first time, then the condition if(tmp==NULL) in the Build function should be true, and hence the program should not enter the else section during the first call to Build. What am I missing here? Could you please help.
class VW
{
World_Obj *head;
public:
void Build(...);
};
void VW::Build(...)
{
World_Obj *newObj;
newObj = new World_Obj;
newObj->SetData(...);
newObj->SetNext(NULL);
World_Obj *tmp;
tmp = this->head;
if (tmp == NULL)
this->head = newObj;
else
{
while (tmp->NextObj() != NULL)
{
tmp = tmp->NextObj();
}
tmp->SetNext(newObj);
}
}
int main()
{
VW *g_w;
g_w = new VW;
/* Reading arguments from input file*/
g_w->Build(...);
}
headshould be a null pointer? You don't show it being initialized anywhere. Please create a Minimal, Complete, and Verifiable Example and show us.