-1
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.

Community
  • 1
  • 1

3 Answers3

1

You are trying to print a pointer to the Employee, that is why you are getting the address. Just dereference the pointer:

cout << "\n[" << counter << "]\n" << *e << "\n";
awesoon
  • 32,469
  • 11
  • 74
  • 99
0

e is a pointer to Employee as defined here:

for (Employee* e : data) {

So if you print, you print an address. If you want to print the value pointed by e, you need to dereference it:

cout << "\n[" << counter << "]\n" << *e << "\n";
//                                   ^-- here
Holt
  • 36,600
  • 7
  • 92
  • 139
0

Your problem is that, you defined your insertion operator as:

ostream& operator<<(ostream& outFile, Employee& emp)

And you call it by passing a pointer...

.....
for (Employee* e : data) {
        cout << "\n["<<counter<<"]\n"<<e<<"\n";
        .....

You should redefine your operator to take Employee by const&:

ostream& operator<<(ostream& outFile, const Employee& emp)

And call it like:

.....
for (Employee* e : data) {
        cout << "\n["<<counter<<"]\n"<< *e<<"\n";
        .....

Also, think about const-correctness and add const to your print memeber function.

virtual void print(ostream& str) const;
WhiZTiM
  • 21,207
  • 4
  • 43
  • 68