Thread: Pointer problem

  1. #1
    Registered User ventolin's Avatar
    Join Date
    Jan 2004
    Posts
    92

    Question Pointer problem

    Hi Im having a few problems with pointers and was wondering whether anyone can point me in the right direction.

    I have the following code:

    Code:
    class LatticeNode {
    
    	public:
    		int num;
    		Cell c;
    
    		LatticeNode *next;
    		LatticeNode *prev;
    	
    		Cell* getCell() { return c; }
    		LatticeNode(Cell *c);
    };
    This forms part of a larger class which forms a simple linked list:

    Code:
    template< class T, int size> class Lattice 
    {
    	public:
    
    		Lattice();
    		~Lattice();
    
    		LatticeNode *getCurrent() { return current; }
    
    		void polarise();
    
    	private:
    
    		LatticeNode* current;
    };
    Now im trying to use the getCell method from LatticeNode to return the object Cell c from the LatticeNode object, like so:

    Code:
    current->getCell()->signal(dt);
    Where signal(dt) is a function in the object Cell. If i call it like this then it crashes, and i dont understand why its causing this..

    Im not sure on the return type for the getCell function .. do i return just c or a reference to &c?

    I believe the Cell object is being created when i call the LatticeNode constructor, although am i pointing to something which doesnt exist??

    Any help, suggestions welcomed!

  2. #2
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Quote Originally Posted by ventolin
    Code:
    class LatticeNode {
    
    	public:
    		int num;
    		Cell c;
    
    		LatticeNode *next;
    		LatticeNode *prev;
    	
    		Cell* getCell() { return c; }
    		LatticeNode(Cell *c);
    };
    You are saying getCell returns a pointer to a cell but you are trying to return the cell itself. Possible changes:
    Code:
    Cell& getCell { return c; }
    Cell* getCell { return &c; }
    Now the top one returns a reference to the cell so you would
    Code:
    current->getCell().signal(dt);
    With the bottom one it returns a pointer so you wouldn't need to change it. I'm surprised your compiler didn't warn about this though.

  3. #3
    Registered User ventolin's Avatar
    Join Date
    Jan 2004
    Posts
    92
    Is there no way to do it without using a reference to the object? or can i still use the pointer method? If i use the reference style i need to major changes to all of my code... and my compiler (VC++) complains if i have the two methods...

  4. #4
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    No you can use the pointer method. Was just giving you options

  5. #5
    Registered User ventolin's Avatar
    Join Date
    Jan 2004
    Posts
    92
    Ok thanks ill take a look at this.. cheers!

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, 08:53 PM
  2. Another pointer problem
    By mikahell in forum C++ Programming
    Replies: 21
    Last Post: 07-20-2006, 07:37 PM
  3. Pointer problem
    By mikahell in forum C++ Programming
    Replies: 5
    Last Post: 07-20-2006, 10:21 AM
  4. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  5. pointer problem
    By DMaxJ in forum C Programming
    Replies: 4
    Last Post: 06-11-2003, 12:14 PM