Thread: issues using ostream while overloading []

  1. #1
    Registered User Mr_Jack's Avatar
    Join Date
    Oct 2003
    Posts
    63

    issues using ostream while overloading []

    I made a header that contains an object that uses linked list that is used for storing strings. My object, called linkedlist, works like this: it makes use of another object called node which contains pointers to the next and previous nodes in the list, an integer that stores the node's "element number", and a character that stores a letter. The linkedlist object contains pointers to nodes that hold the addresses of the first node in the list, the last node in the list, and the node it is currently "on". It also has a ton of methods for changing the current pointer to the next node in the list etc., and operator functions to make console output and input easier and some other ones. Look at the source code below:

    Code for linkedlist.h:
    Code:
    #ifndef linkedlist_h
    #define linkedlist_h
    
    #include<iostream>
    using namespace std;
    
    int LLstrlen(char[]);
    int LLstrlen(char str[])
    {
    	int i = 0;
    	while(str[i] != '\0')
    		i++;
    	return i;
    }
    
    class node
    {
    private:
    	int elementNum;
    	node * next;
    	node * prev;
    	char ch;
    public:
    	friend class linkedlist;
    	operator=(char);
    	node();
    	friend ostream& operator<<(ostream&, node&);
    };
    node::node()
    {
    	next = 0;
    	prev = 0;
    }
    node::operator=(char c)
    {
    	this->ch = c;
    }
    ostream& operator<<(ostream & os, node & n)
    {
    	os << n.ch;
    	return os;
    }
    
    class linkedlist
    {
    private:
    	node * head;
    	node * current;
    	node * tail;
    	int len;
    public:
    	friend ostream& operator<<(ostream&, linkedlist&);
    	linkedlist();
    	void clearList();
    	void calcLen();
    	int getLen();
    	void createNext();
    	void gotoNext();
    	void gotoPrev();
    	void gotoHead();
    	void gotoTail();
    	void gotoElement(int);
    	void setCurCh(char);
    	char getCurCh()const;
    	void setCurElementNum(int);
    	char * createString();
    	operator=(char[]);
    	node operator[](int);
    	friend void operator>>(istream&, linkedlist&);
    };
    node linkedlist::operator[](int e)
    {
    	gotoElement(e);
    	return *current;
    }
    ostream& operator<<(ostream & os, linkedlist & ll)
    {
    	ll.gotoHead();
    	for(int i = 0; i < ll.len; i++)
    	{
    		os << ll.getCurCh();
    		ll.gotoNext();
    	}
    	return os;
    }
    linkedlist::operator=(char str[])
    {
    	clearList();
    	int len = LLstrlen(str);
    	for(int i = 0; i < len; i++)
    	{
    		setCurCh(str[i]);
    		setCurElementNum(i);
    		createNext();
    		gotoNext();
    	}
    	this->len = len;
    }
    void operator>>(istream& is, linkedlist& ll)
    {
    	ll.clearList();
    	char ch;
    	is.get(ch);
    	ll.setCurCh(ch);
    	ll.setCurElementNum(0);
    	int len = 0;
    	int i = 1;
    	while(ch != '\n')
    	{
    		is.get(ch);
    		ll.createNext();
    		ll.gotoNext();
    		ll.setCurCh(ch);
    		ll.setCurElementNum(i);
    		len++;
    		i++;
    	}
    	ll.len = len;
    }
    linkedlist::linkedlist()
    {
    	head = new node;
    	current = head;
    	len = 0;
    	tail = current;
    }
    void linkedlist::clearList()
    {
    	node * temp;
    	gotoTail();
    	for(int i = 0; i < len-1; i++)
    	{
    		temp = tail->prev;
    		delete current;
    		current = temp;
    	}
    	head = current;
    	tail = current;
    }
    void linkedlist::calcLen()
    {
    	if(len == 0)
    	{
    		while(current->next != 0)
    		{
    			len++;
    			current = current->next;
    		}
    	}
    }
    int linkedlist::getLen()
    {
    	return len;
    }
    void linkedlist::createNext()
    {
    	current->next = new node;
    	current->next->prev = current;
    }
    void linkedlist::gotoNext()
    {
    	current = current->next;
    }
    void linkedlist::gotoPrev()
    {
    	current = current->prev;
    }
    void linkedlist::gotoHead()
    {
    	current = head;
    }
    void linkedlist::gotoTail()
    {
    	current = tail;
    }
    void linkedlist::gotoElement(int n)
    {
    	gotoHead();
    	while(current->elementNum != n)
    		gotoNext();
    }
    void linkedlist::setCurCh(char c)
    {
    	current->ch = c;
    }
    void linkedlist::setCurElementNum(int n)
    {
    	current->elementNum = n;
    }
    char linkedlist::getCurCh()const
    {
    	return current->ch;
    }
    char * linkedlist::createString()
    {
    	calcLen();
    	char * str = new char[len];
    	str[len] = '\0';
    	gotoHead();
    	for(int i = 0; i < len; i++)
    	{
    		str[i] = current->ch;
    		gotoNext();
    	}
    	return str;
    }
    #endif
    My problem is when I try to do this:
    Code:
    cout << linkedlistVar[0];
    I get an illegal operand error. As you might have seen in my code above, linkedlist's operator function for [] returns a node. node has an operator function for using ostream. Why doesn't this work? I know that the operator[] function for linkedlist works because I said
    Code:
    node myNode = myLinkedList[0];
    cout << myNode;
    and the output was the contents of the first node of my linkedlist.

    Please help me figure out why
    Code:
    cout << linkedlistVar[0];
    doesn't work.

    edit: wherever a yawning smilie appears, mentaly replace it with "". I don't know how to stop this parser from doing that.

    edit: damn it. Okay replace with ": o" mentaly, only without the space in between.
    Last edited by Mr_Jack; 01-12-2004 at 11:51 AM.

  2. #2
    Registered User
    Join Date
    May 2003
    Posts
    161
    It's probably because you're passing a reference to a temporary. Change your overload function to a const reference and it should work:
    Code:
    ostream& operator<<(ostream & os, const node & n)
    Make sure to change the friend declaration too.

    Btw, when you're posting, there is an option called "Disable Smillies in This Post" under the reply textbox.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help on understandind arrays
    By C++mastawannabe in forum C++ Programming
    Replies: 9
    Last Post: 06-16-2007, 10:50 PM
  2. Overloading [] operator for a vector
    By nappaji in forum C++ Programming
    Replies: 4
    Last Post: 05-01-2007, 10:44 AM
  3. overloading []
    By pktcperlc++java in forum C++ Programming
    Replies: 3
    Last Post: 03-02-2005, 09:09 PM
  4. Stupid compiler errors
    By ChrisEacrett in forum C++ Programming
    Replies: 9
    Last Post: 11-30-2003, 05:44 PM
  5. Overloading the [] operator trouble
    By rmullen3 in forum C++ Programming
    Replies: 2
    Last Post: 08-01-2002, 12:46 PM