Passing object by reference

I have this method that takes a pointer to a class object and right now its 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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
1
2
3
void Employee::addSale(Insurance *i){
    sale_list.push_back(i);  // would like to pass i by reference (&i)
}


When I call it in the main I do so like this
1
2
3
4
5
6
7
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
1
2
3
4
5
6
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
1
2
3
4
5
6
7
8
9
10
11
12
13
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;
}
Last edited on
The first two snippets look fine.

main.cpp line 4 is bogus. You're trying to pass an Insurance object (*a), but addSale is expecting a pointer to an Insurance object (just a). Since this would cause a compile error, I'm assuming this is a typo.

What do you mean :returning the location in memory? addSale is a void function. It doesn't return anything. Do you mean the value of e is 0x100300000 ?

Also, I see nothing in your code about returning something by reference, which is what you state in your title.

Other than those comments, there's not enough information here.

Last edited on
Yes that was a typo, and it would return 0x100300000 when i printed it, I didn't put the print function in this example because i didnt think it was needed. And i meant i wanted to return the reference in addSale(method). Like sale_list.push_back(&i); but that doesnt work..
manager.cpp line 8 is suspicious. temp is a pointer (assuming it's not a typo), so you're going to output the value of the pointer.

What you want to print is the object, not the pointer:
 
  strm << *temp << endl;  // Note the * to dereference the pointer 

Ahh that worked, I tried that earlier but I mixed up the uses of * and &. I implemented it with & instead. Thank you, I think I need to brush up more on my knowledge of pointers haha!
Topic archived. No new replies allowed.