سؤال

Hello and thanks in advanced.

Here is my issue. I have these classes:

-class Date -class Contract -class Employee -class Node -class List

All of the classes derive from this class BaseObject. It has this code:

#ifndef BASEOBJECT_H
#define BASEOBJECT_H
#include<iostream>
using namespace std;

class BaseObject{
public:
virtual string toString() const=0;
virtual BaseObject* clone() const=0;
};

ostream& operator << (ostream&, objetoBase&);


#endif  /* BASEOBJECT_H */

That's all it has. I'm skipping the operator << implementation. Now, I'll explain the idea

Class Date has just elemental attributes (int day, int month, int year). Class Contract has two class Date pointers so Contract is related to Date by composition. Class Employee has an association with one Contract handled with pointer.

Class Node has two attributes: the first is a pointer to a Node which contains an ObjectBase instance called Data, the second one is a pointer to another Node instance called nextNode.

Class List handles the node pointers in a simple list.

My question: When I am looking for an Employee in the list and use a Find() function to have acces to the Data contained in Node, it now just shows the methods created in BaseObject! How can I solve this? I leave the code to you. The first and current are nodes.

BaseObject * List :: find (string id) {
        if (first == NULL)
            return NULL;
        else
        if (first-> getData () -> == getId() id) //This line is underlined in compiler
            return first-> getData ();
        else {
            current = first;
        if (current-> getNextNode () == NULL)
            return NULL;
        else {
        while (current-> getNextNode () -> getNextNode ()! = NULL && Current-> getNextNode () -> getData () -> getId ()! = id) {
            current = current-> getNextNode ();}
        if (current-> getNextNode () -> getData () -> getId () == id)
            return current-> getNextNode () -> getData ();
        else
            return NULL;
        }
    }
}

The underlined code makes the error. When I use the compiler and put the "->" sign it only shows BaseObject methods Clone() and toString() and I want to make use of the Employees methods although it is now a BaseObject pointer with an Employee instance within it.

Please help me if I expressed me well.

هل كانت مفيدة؟

المحلول

You can change a little your implementation and create a Comparable interface and all of your BaseObject's are comparable.

Or you can pass a "comparator" function in your find method. In this function you will need to do some casts to verify if your instance is an Employer and compare de Id's.

I hope this can help.

Pedro

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top