Thread: SO new to C++

  1. #1
    Registered User
    Join Date
    Dec 2006
    Posts
    40

    SO new to C++

    I feel bad submitting such a novice question but I really am having a hard time with this linked list program I am working on. My main question is what I'm doing wrong on the function in red. As a side note, if you see anything else that I've done that is incorrect, please feel free to comment! I'm a good java programmer and am trying to learn C++. So if that background helps, feel free to use it! Thanks in advance for the help!

    -Dustin

    Code:
    #include <string>
    
    //! LLNode implements a doubly-linked list node
    class LLNode {
    	friend class LinkedList;  //!< LinkedList can access private members of LLNode
    public:
    
    	//!  Constructor
    	LLNode(const std::string & v, LLNode * p, LLNode * n) :
    	  value(v), prev(p), next(n)
    	{
    	}
    	
    	//! Copy Constructor SHALLOW COPY and does not free memory
    	//!Feel free to change this function(IE, to a deep copy).
    	LLNode(const LLNode & other) : 
    	   value(other.value),prev(other.prev),next(other.next)
    	{
    	}
    
    	//!  Read-only public methods for use by clients of the LinkedList class
    	const std::string & GetValue() const
    	{
    	  return value;
    	}
    
    
    	LLNode * GetPrevious()const
    	{
    	  return prev;
    	}
    
    
    	LLNode * GetNext()const
    	{
    	  return next;
    	}
    	
    	//! Assignment operator makes a SHALLOW COPY and does not free memory
    	//!Feel free to change this function(IE, to a deep copy).
    	LLNode & operator=(const LLNode & other)
    	{
    		if(this!=&other)
    		{
    			value=other.value;
    			prev=other.prev;
    			next=other.next;
    		}
    		return *this;
    	}
    
    private:
    	std::string value;        //!< value stored in the node
    	LLNode * prev;            //!< pointer to previous node in the list
    	LLNode * next;            //!< pointer to next node in the list
    };
    
    
    //! LinkedList implements a doubly-linked list
    class LinkedList
    {
    public:
    
    	//!  No-arg constructor.  Initializes an empty linked list
    	LinkedList() : LLRoot("", NULL, NULL)
    	{	}
    
    	//!  Copy constructor.  Makes a complete copy of its argument
    	LinkedList(const LinkedList & other):LLRoot(other.LLRoot)
    	{	}
    
    	//!  Destructor
    	~LinkedList();
    
    
    	//! Assignment operator.  Makes a complete copy of its argument
    	//! @return A reference to oneself
    	LinkedList& operator =(const LinkedList & other)
    	{
    		return LinkedList(other);
    	}
    
    
    	//!  @return true if the list is empty, or false if the list is not empty
    	bool IsEmpty() const
    	{
    		if(LLRoot.value=="")
    			return true;
    		return false;
    	}
    
    
    	//!  Removes all values from the list
    	void Clear()
    	{
    		LLRoot.value="";
    		LLRoot.next=NULL;
    	}
    
    
    	//!  @return the number of values in the list
    	int GetSize() const
    	{
    		int size=0;
    		LLNode LLCurrent(LLRoot);
    
    		while(LLCurrent.GetValue()!="" && LLCurrent.GetValue()!="\0")
    		{
    			size++;
    			LLCurrent = LLNode(*LLCurrent.next);
    		}
    		return size;
    	}
    
    
    
    	//!  @return a pointer to the first node in the list, or NULL if the list is empty
    	LLNode * GetFirst()const
    	{
    		if(LLRoot.value=="")
    			return NULL;
    		return 
    			*LLRoot;
    	}
    
    
    
    	//!  @returns a pointer to the last node in the list, or NULL if the list is empty
    	LLNode * GetLast()const;
    
    
    	//!  Inserts value v into the list after node n
    	//!  
    	//!  @param v The new value being inserted
    	//!  @param n A node that is already in the list after which the new node should 
    	//!      be inserted.
    	//!      If n is NULL, the new node should be inserted at the beginning of the list.
    	//!
    	//!  @return a pointer to the newly inserted node
    	LLNode * Insert(const std::string & v, LLNode * n);
    
    
    	//! Searches for the first occurrence of value v that appears in the list 
    	//!   after node n
    	//!   
    	//!  @param v The value being searched for
    	//!  @param n The node in the list after which the search should begin.
    	//!      If n is NULL, the list should be searched from the beginning.
    	//!
    	//!  @return a pointer to the node containing v, or NULL if v is not found
    	LLNode * Find(const std::string & v, LLNode * n) const;
    
    
    	//!  Removes node n from the list
    	//!  
    	//!  @param n The node being removed from the list
    	void Remove(LLNode * n);
    
    private:
    	LLNode LLRoot;
    };

  2. #2
    Registered User
    Join Date
    Dec 2006
    Posts
    40
    If it helps the compiler gives me:

    Code:
    error C2440: 'return' : cannot convert from 'const LLNode' to 'LLNode *'
    when I try to compile.

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Since you need to return a pointer you should return a pointer. LLRoot is a pointer, so return it.

  4. #4
    Registered User
    Join Date
    Dec 2006
    Posts
    40
    With:
    Code:
    //!  @return a pointer to the first node in the list, or NULL if the list is empty
    	LLNode * GetFirst()const
    	{
    		if(LLRoot.value=="")
    			return NULL;
    		return LLRoot;
    	}
    I get:

    Code:
    error C2440: 'return' : cannot convert from 'const LLNode' to 'LLNode *'

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Okay so I skimmed, and you managed to implement LLRoot as a Node itself, not a Node *. In that case, you would need to return the address of LLRoot, by returning &LLRoot.

    However, in my defense, it is impossible to get the error message you stated from the code you posted. If LLRoot is not a Node *, but a Node, as you have it defined, then trying to do *Node is a syntax error
    Code:
    error: no match for ‘operator*’ in ‘*((const LinkedList*)this)->LinkedList::LLRoot'
    since you cannot follow a pointer if the thing you are trying to follow is not, in fact, a pointer.

  6. #6
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    If you don't intend to allow modifications to the node that you return then you'll need to return a const pointer. Otherwise you'll need to make the method non-const.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed