-
Pointer problem
I have 2 structs as follows:
Code:
template <class T>
struct VList
{
T wrd;
VList<T> *nextVList, *nextRight;
Vertex<T> *vTmpPtr;
};
template <class T>
struct Vertex
{
T wrd;
int pathLength;
Vertex<T> *nextVertex;
};
I need to do the following :
vListHead->nextRight=tmp->vTmpPtr;
The error I receive is :
Cannot convert 'Vertex<String> *' to 'VList<String> *' in function Graph<String>::adjacencyList(Vertex<String> &)
Is there anyway I can do this??
-
sorry...forgot to put the function itself
Code:
void Graph<T>::adjacencyList (Vertex<T>& elem)
{
VList<T> *tmp;
tmp->vTmpPtr=new Vertex<T>;
if (!matchRes)
{
tmp->vTmpPtr->wrd=elem.wrd;
tmp->vTmpPtr->pathLength=elem.pathLength;
tmp->vTmpPtr->nextVertex=NULL;
vListHead->nextRight=tmp->vTmpPtr; <-----------
vertexPtr=tmp->vTmpPtr;
}
else
{
tmp->vTmpPtr->wrd=elem.wrd;
tmp->vTmpPtr->pathLength=elem.pathLength;
tmp->vTmpPtr->nextVertex=NULL;
vertexPtr->nextVertex=tmp->vTmpPtr;
vertexPtr=vertexPtr->nextVertex;
}
}
-
Doing
VList<T> *tmp;
tmp->vTmpPtr=new Vertex<T>;
shouldn't work (and may crash if you get your code to compile), as tmp is an unallocated pointer. You could try something like -
tmp = new VList<T>;
proceed as before. Then do
vListHead->nextRight=tmp;
in place of
vListHead->nextRight=tmp->vTmpPtr;
-
By doing what you have suggested, I am creating a new node of the struct VList....but my problem is that I have a node from VList struct and a node from Vertex struct and I need the nextRight ptr from VList to point to the node from Vertex.
Code:
______ ______
[____|_] -----> [____|_]
VList Node Vertex Node
-
But, in your definition of a VList, the next right pointer points to a VList, not a Vertex node. The Vertex node is pointed to by your added VList node.
-
:confused: can you show me how I can get the VList nextRight ptr to point to the vTmpPtr....sorry for this
-
Copied from your example -
Code:
VList<T> *tmp = new VList<T>;
tmp->vTmpPtr=new Vertex<T>;
if (!matchRes)
{
tmp->vTmpPtr->wrd=elem.wrd;
tmp->vTmpPtr->pathLength=elem.pathLength;
tmp->vTmpPtr->nextVertex=NULL;
vListHead->nextRight=tmp;
//Don't know what vertexPtr is
vertexPtr=tmp->vTmpPtr;
}
//etc
If you've got something else in mind then you're going to have to rethink your data structures.