4 Replies - 363 Views - Last Post: 02 August 2014 - 01:50 PM Rate Topic: -----

#1 dombavetta  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 11-July 14

Returning reference to an object

Posted 01 August 2014 - 02:57 PM

I have this method that takes a pointer to a class object and right now when printing it, it's returning the location in memory 0x100300000. I've tried tweaking the function in a few different ways but I cant get it to return the object instead of the location.


Here's the vector that addSale accesses and the deceleration of the addSale ethod in the employee class.
class Employee{
protected:
    ....
public: 
    vector<Insurance *> sale_list; // holders pointers to the abstract Insurance class objects
    // Adds sale object to a list of all that empoyees sales
    void addSale( Insurance * );
    virtual ostream &print_employee_sales( ostream & ) = 0;
};

namespace printSales {
    ostream &operator<<( ostream &, Employee & );
}




Defintion of addSale
void Employee::addSale(Insurance *i){
    sale_list.push_back(i);
}



When I call it in the main I do so like this
int main() {
    Insurance *a = new Auto("John", "Smith", "ford", "mustang", 123456, 50, 76);
    Employee *e = new Manager("Bob", "Smith", 32000);
    e->addSale(a);
    printSales::operator<<(cout, *e); // returns  0x100300000
return 0;
}



UPDATE: this is what one of the print functions looks like
This is found in Employee.cpp
ostream &printSales::operator<<( ostream &strm, Employee &e ){
    // Virtual print
    cout<< "Employee Sales" << endl;
    return e.print_employee_sales( strm );
}




and this in in Manager.cpp
ostream &Manager::print_employee_sales( ostream &strm ){
    strm << "-Sale information- " << endl;
    strm << "  List of Sales : " << endl;
        // Prints the stored list of sales for that employee
        Insurance *temp;
        for(int i = 0; i < sale_list.size(); ++i){
            temp = sale_list.at(i);
            strm<< temp << endl;
        }
    
    strm<< endl;
    return strm;
}



Is This A Good Question/Topic? 0
  • +

Replies To: Returning reference to an object

#2 snoopy11  Icon User is online

  • Engineering ● Software
  • member icon

Reputation: 774
  • View blog
  • Posts: 2,294
  • Joined: 20-March 10

Re: Returning reference to an object

Posted 01 August 2014 - 05:08 PM

You have some conceptual errors in your thinking concerning operator overloading.

Consider this small example.

#include <iostream>
#include <vector>
#include  <string>

using namespace std;

class Employee
{
    public:
    Employee(string name, double salary);
    
    friend ostream &operator<<(ostream &out, const Employee* Other )
    {
    out << "Employees  Name: "  <<Other->Name<<   endl;
    out << "Yearly Salary $: "  <<Other->Salary<< endl;
    return out;
    }
    
    private:
    string Name;
    double Salary;
};

Employee::Employee (string name, double salary)
{
    Salary = salary;
    Name = name;

}





int main()
{
    Employee *bob = new Employee("bob smith", 64135.56);

    cout << bob;
    return 0;
}




We have a class Employee that takes a constructor and assigns values etc in the usual way.

We have a friend function for the output stream and we overload the << operator in the form of out taking a pointer to Employee in which we return ostream& out after we load up information into it..

Now using the << operator is easy just cout << 'object'; in this case 'bob'.

Its much easier than what you are trying to do.

Snoopy.
Was This Post Helpful? 0
  • +
  • -

#3 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2113
  • View blog
  • Posts: 3,235
  • Joined: 21-June 11

Re: Returning reference to an object

Posted 01 August 2014 - 05:11 PM

Just dereference the pointer when you print it.
Was This Post Helpful? 0
  • +
  • -

#4 dombavetta  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 11-July 14

Re: Returning reference to an object

Posted 02 August 2014 - 01:37 PM

View Postsepp2k, on 01 August 2014 - 05:11 PM, said:

Just dereference the pointer when you print it.


I couldn't get that to work :/
Was This Post Helpful? 0
  • +
  • -

#5 dombavetta  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 11-July 14

Re: Returning reference to an object

Posted 02 August 2014 - 01:50 PM

View Postdombavetta, on 02 August 2014 - 01:37 PM, said:

View Postsepp2k, on 01 August 2014 - 05:11 PM, said:

Just dereference the pointer when you print it.


I couldn't get that to work ://>


Scratch that, I mixed up my reference and dereference operator >.< Silly me haha, thanks for the help!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1