Hello,

I am having a problem when I am trying to create a new node. The problem is on line:

tnode->lc= new cttNode;

Here is the template the problem resides in:

template <class B>

int ctt<B> :: insertIfNew(const string &key, const B &index)

{

cttNode<B> *tnode= root;

int i=0;

tnode=matchChar(tnode, key[i]);

if(tnode== key[i])

	{

	for(i; i <key.size()-1; i++)

		{

		tnode=matchChar(tnode, key[i]);

		if(tnode != key[i])

			break;

		}

	if (key[key.size()-1]== tnode->cVal)

		{

		if (!tnode->hasIndex)

			{

			tnode->hasIndex=1;

			tnode->index=index;

			}

		else if(tnode->hasIndex)

			return 42;

		}	

	}

if(tnode != NULL)

	{

	if(key[i] > tnode->cVal)

		{

		tnode->rc= new cttNode;

		tnode->rc->cValue=key[i];

		tnode->rc->par=tnode;

		tnode=tnode->rc;

		tnode->rc=tnode->lc=NULL;

		i++;

		}

	else if (key[i] < tnode->cVal)

		{

		tnode->lc= new cttNode;

		tnode->lc->cValue=key[i];

		tnode->lc->par=tnode;

		tnode=tnode->lc;

		tnode->rc=tnode->lc=NULL;

		i++;

		}

	for (i; i < key.size()-1; i++)

		{

		tnode->cc= new cttNode;

		tnode->cc->cValue=key[i];

		tnode->cc->par=tnode;

		tnode=tnode->cc;

		tnode->rc=tnode->lc=NULL;

		}

	}

else

	{

	root->cValue=key[i];

	tnode=root;

	for (i+1; i < key.size()-1; i++)

		{

		tnode->cc= new cttNode;

		tnode->cc->cValue=key[i];

		tnode->cc->par=tnode;

		tnode=tnode->cc;

		tnode->rc=tnode->lc=NULL;

		}

	}

tnode->hasIndex=1;

tnode->index=index;

return 0;

}

Is insertIfNew called in the constructor?

I'd suggest you make the shortest possible compilable code that demonstrates the problem.

"I am having a problem" should be converted to what the problem is and where/when it occurs.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.