CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Dec 2009
    Location
    USA,PA
    Posts
    26

    pointers and functions

    I've been having a bit of trouble with a piece of code, where I'm trying to call a function on the object being pointed to.

    I'm afraid I may be using these incorrectly (I've been programming in c# for the last year or so, and am pretty rusty with c++ pointers) and I would welcome any help or insight anyone may be able to provide.

    I will post three snippits of code, first a function which is giving me an issue (it is either telling me the left hand operand must be a struct/class/union, or if i use the deference operator it tells me that I have a syntax error '.') the second will the be trinaryGraph specification file and the third will be my node specification file. The code is a bit sloppy, It is meant to be used with a few other data containers not yet finished.


    ~Problem Code
    Code:
    template <class item>
    Node<item> TrinaryGraph<item>::getNodeByPos(int x,int y,int z)//will be cleaned up by a data container containing 3 float values
    {
    	Node<item> *tempNode;
    	tempNode=rootNode;
    
    	if (x==0 && y==0 && z==0)
    		return *rootNode;
    
    	while(int iX=0 <= x)
    	{
    		if (tempNode.getLeftNode()==NULL)
    			return NULL;
    		tempNode=tempNode.getLeftNode();
    		iX++;
    	}
    	
    	while(int iY=0 <= y)
    	{
    		if (tempNode.getDownNode()==NULL)
    			return NULL;
    		tempNode=tempNode.getDownNode();
    		iY++;
    	}
    
    
    	while(int iZ=0 <= z)
    	{
    		if (tempNode.getRightNode()==NULL)
    			return NULL;
    		tempNode=tempNode.getRightNode();
    		iZ++;
    	}
    
    	return tempNode;
    
    }
    ~TrinaryGraph Specification
    Code:
    template <class item>
    class TrinaryGraph
    {
    public:
    
    	///////CONSTRUCTORS///////
    
    	TrinaryGraph(int xDim,int yDim,int Zdim, float step=1);
    
    	~TrinaryGraph();
    
    
    	/////MODIFICATION MEMBER FUNCTIONS/////////
    
    	Node<item> getNodeByPos(int x,int y,int z);
    	void createGraph(Node<item> current,int x=0,int y=0,int z=0);
    
    
    	//data to fill must be hardcoded at this time
    	void fillGraphFaces(int x=0,int y=0,int z=0);
    	
    	void fillGraph(Node<item> currentNode);
    	void fillNodeAtLocation(int x,int y,int z);
    
    
    	void swap(Node<item> first,Node<item> second);
    	void swap(int x1,int y1, int z1, int x2, int y2, int z2);
    
    
    	///TODO: faux random-access, matching(recursively)
    private:
    	struct dims
    	{
    		int x,y,z;
    	};
    
    	dims dimensions;
    
    	Node<item>* rootNode;
    
    	void fillGraphFaces(Node<item> currentNode, int x,int y,int z);
    
    };
    ~Node specification
    Code:
    template <class item>
    class Node
    {
    public:
    
    	/////CONSTRUCTORS//////
    	Node();
    	Node(item idata);
    
    	~Node();
    
    	//MODIFICATION MEMBER FUNCTIONS///
    	void setData(item data);
    
    	void setLeftNode(Node<item> newNode);
    	void setDownNode(Node<item> newNode);
    	void setRightNode(Node<item> newNode);
    
    	void createLeftNode(item idata);
    	void createDownNode(item idata);
    	void createRightNode(item idata);
    
    	void createLeftNode( );
    	void createDownNode( );
    	void createRightNode( );
    
    	void operator = (Node<item> source);
    	void Node<item>::operator == (Node<item> source);
    
    	//CONSTANT MEMBER FUNCTIONS//
    	item getData() const;
    	Node<item> getLeftNode() const;
    	Node<item> getDownNode() const;
    	Node<item> getRightNode() const;
    
    
    
    private:
    	item data;
    	Node<item>* leftNode;
    	Node<item>*downNode;
    	Node<item>* rightNode;
    	
    
    };
    I thank you for your time.

  2. #2
    Join Date
    Jun 2009
    Location
    oklahoma
    Posts
    199

    Re: pointers and functions

    You declare tempNode as a pointer and to access it you use ->
    However, you are
    Code:
    tempNode=tempNode.getLeftNode();
    getLeftNode() does not return a pointer according to your Node specification, thats why you are getting an error. If you change it to
    Code:
    tempNode=&tempNode->getLeftNode();
    It will compile, but this is probably not what you want.
    Generally when dealing with nodes, everything is pointers.

  3. #3
    Join Date
    Dec 2009
    Location
    USA,PA
    Posts
    26

    Re: pointers and functions

    wonderful, that would most likely be my oversight, thank you very much. Ill try changing that first thing in the morning.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured