I have done the following operator overloading which works as expected on the following class object. But when I pass this pointer to class object in the vector, the output stream doesn't work (in the manner I defined it)
I'm sorry for this type of question but I am new in C++ and I don't know how to ask this exactly.
Operator Overloading:(shape.cpp)
ostream &operator<<(ostream &output, Shape &S)
{
output << S.name << " (" << S.id << ") " << endl;
return output;
}
Inside Shape Class(shape.h): friend ostream& operator<<(ostream& output, Shape &S);
main.cpp:
#include <iostream>
#include <vector>
using std::cout;
int main()
{
vector<Shape*> array;
Shape * s1 = new Shape("Square");
array.push_back(s1);
cout << *s1; //Prints "Square (1)"
cout << array[0]; //Prints "007CADC8" maybe hex address of vector element?
return 0;
}
Aucun commentaire:
Enregistrer un commentaire