Pointer problem

This is a discussion on Pointer problem within the C++ Programming forums, part of the General Programming Boards category; I have 2 structs as follows: Code: template <class T> struct VList { T wrd; VList<T> *nextVList, *nextRight; Vertex<T> *vTmpPtr; ...

  1. #1
    Unregistered
    Guest

    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??

  2. #2
    Unregistered
    Guest
    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;
        }
    }

  3. #3
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    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;

  4. #4
    Unregistered
    Guest
    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

  5. #5
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    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.

  6. #6
    Unregistered
    Guest
    can you show me how I can get the VList nextRight ptr to point to the vTmpPtr....sorry for this

  7. #7
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. pointer to pointer realloc problem
    By prakash0104 in forum C Programming
    Replies: 14
    Last Post: 04-06-2009, 09:53 PM
  2. Another pointer problem
    By mikahell in forum C++ Programming
    Replies: 21
    Last Post: 07-20-2006, 08:37 PM
  3. Pointer problem
    By mikahell in forum C++ Programming
    Replies: 5
    Last Post: 07-20-2006, 11:21 AM
  4. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 08:48 PM
  5. pointer problem
    By DMaxJ in forum C Programming
    Replies: 4
    Last Post: 06-11-2003, 01:14 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21