mercredi 1 juin 2016

Overloading operator<< is not working

class Employee
{
public:
    Employee(const string& fName, const string& lName, float salary) {
        mSalary = salary;
        mFirstName = fName;
        mLastName = lName;
    }

    static Employee create() {
        string input1, input2; 
        float sal; 
        cout << "Enter first name: ";
        cin >> input1;
        cout << "\nEnter last name: ";
        cin >> input2;
        cout << "\nEnter salary here: ";
        cin >> sal;
        cout << "\n";
        Employee emp(input1, input2, sal);
        return emp;
    }

    virtual void printStats() {
        cout << "===============================================\n";
        cout << "First name:\t\t" << mFirstName << endl;
        cout << "Last name:\t\t" << mLastName << endl;
        cout << "Salary:\t\t\t" << mSalary << endl;
    }

    friend ostream& operator<<(ostream& outFile, Employee& emp) {
        emp.print(outFile);
        return outFile;
    }


    virtual string getName() {
        return mFirstName;
    }

protected:
    virtual void print(ostream& str) {
        str << "Name: " << mFirstName << " " << mLastName << endl;
        str << "Salary" << mSalary << endl;
    }
    string mFirstName;
    string mLastName; 
    float mSalary;
};

In seperate class called database, I have this method:

void showEmployees() {
        int counter = 1;
        for (Employee* e : data) {
            cout << "\n["<<counter<<"]\n"<<e<<"\n";
            counter++;
        }
    }

When I use this method I just get the memory address. Also I know the implementations are in the header file (I was just lazy).

Making operator<< virtual?

I followed this advice here so that I can effectively insert employee into an ostream object but it just gives me a memory address...I get that returning ostream& will give me an address but I don't know what else I could do that would work.

Aucun commentaire:

Enregistrer un commentaire