I am working a program that imports functions from a file, and stores them as Polynomial objects in a linked list. I have been given a linked list class already built. But i have to make a Database class that performs other operations. I am stuck on one that requires operator overloading.

i need to overload the [] operator for my Database class to call the nth polynomial in the linked list. so that i can perform Polynomial functions on the nth Polynomial in the linked list.

I need to be able to do this:

d[2].print()

with print being a function of the Polynomial class!

the below function is what i have to make

Polynomial& Database::operator[] (int)
{
       // not sure what goes here

}

I dont want a whole solution, just an idea of where to start. If any more information is needed to make this clearer let me know.

>> // not sure what goes here

Neither do we since you didn't post enough of the class to know what is supposed to be returned. Its supposed to return a reference to something, but we have no idea what.

This is a risky venture because people expect array notation to have random access performance characteristics. Linked lists are not random access, and simulating random access is not only relatively expensive, it becomes more expensive as the list grows and the subscripts get larger:

Polynomial& Database::operator[] ( int i )
{
    node *it = head;

    while ( --i >= 0 )
        it = it->next;

    return it->data;
}

ok here is the linked list class that i have and a part of the database class.

class LinkedList {
  protected:
    Polynomial* first;
    int count;
  public:
    LinkedList(void); 
    int getCount(void);
    void setCount(int);
    void insert(Polynomial*);
    void remove(int);
    Polynomial& getPoly(int); 
};

LinkedList::LinkedList(void) {
	
  first=NULL;
  count=0;
}

int LinkedList::getCount(void) {
  return count;
}

void LinkedList::setCount(int n) {
  count=n;
}

void LinkedList::insert(Polynomial* x) {
  Polynomial* newone=x;
  if(first==NULL){
     first=newone;
     first->setNext(NULL);
     count=1;
  }
  else{
    Polynomial* p=first;
    while(p->getNext()) p=p->getNext();
    p->setNext(newone);
    newone->setNext(NULL);
    count++;
  }
}

void LinkedList::remove(int n) {
  if(n > getCount()) return;

  Polynomial* pdel;
  if(n==1){
    pdel=first;
    first=pdel->getNext();
    delete pdel;
    count--;
  }
  else{
    Polynomial* p=first;
    for(int i=1;i<n-1;i++){
      p=p->getNext();
    }
    pdel=p->getNext();
    p->setNext(pdel->getNext());
    delete pdel;
    count--;
  }
}

Polynomial& LinkedList::getPoly(int n) {
  if(n==1) return *first;

  Polynomial* p=first;
  for(int i=0;i<n-1;i++){
    p=p->getNext();
  }
  return *p;
}


class Database: public LinkedList {
  private:
    // nothing here
  public:
    Database(void);
    bool isEmpty(void);
    Polynomial& operator[](int);
    void display(void);
    void clear(void);
};

Database::Database(void)
{

}

bool Database::isEmpty()
{
	bool empty = false;
	if (first == NULL)
	{
		empty = true;
	}
	return empty;
}

Polynomial& Database::operator[] (int n)
{
	


}

The function needs to return a reference to a Polynomial object which is another class that i have built already.

Considering that you have already have defined LinkedList::getPoly(int n)
Your operator[] should look like this.

Polynomial& Database::operator[] (int n)
{
	return getPoly(n);
}

Looks like all it has to do is return the value returned by getPoly()

[edit]What ^^^ said too :) [/edit]

wow i didnt even see that haha. thanks! :-O

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.