hit*_*ter 0 c++ vector operator-overloading c++11
我已经完成了以下运算符重载,它在预期的下面的类对象上工作.但是当我将此指针传递给向量中的类对象时,输出流不起作用(以我定义的方式)
对于这类问题我很抱歉,但我是C++的新手,我不知道如何准确地提出这个问题.
运算符重载:(shape.cpp)
ostream &operator<<(ostream &output, Shape &S)
{
output << S.name << " (" << S.id << ") " << endl;
return output;
}
Run Code Online (Sandbox Code Playgroud)
内部形状类(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;
}
Run Code Online (Sandbox Code Playgroud)
这会失败,因为您正在输出a Shape *,类似于您所做的情况cout << s1.您需要执行与前一行中相同的操作并取消引用向量中包含的指针:
cout << *array[0];
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
167 次 |
| 最近记录: |