Operator overloading passing pointer

I've got a class called Stock and within that class i have a an operator overloader for cout (operator<<) and it works if i pass it an Stock object, but if i pass it a Stock pointer, cout just prints the address of the pointer rather than calling the overloaded operator. Is this expected?

Here is the prototype

1
2
3
4
5
6
7
8
friend std::ostream & operator<<(std::ostream &os, Stock & st);

//main program
Stock st1;
Stock * st2;
... more code
cout << st1; //works calls the overloaded operator
cout << st2; //just prints the address, doesnt call the overloaded operator. 
Last edited on
cout << *st2; //Dereference a ponter before using it
Or you can overload operator<< for pointer to Stock data type.
Yeah i thought of that, but that doenst work either. When i dereference it i get

error: no match for operator<< in std::cout << * st2

I also created this overloaded operator, but it doesnt call it
 
friend std::ostream & operator<<(std::ostream &os, Stock * st);
Then there there is problem in your code which isn't shown.
This should work.
Topic archived. No new replies allowed.