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