我有这个类:C++ “<<” 操作符重载
// in Platform.h
class Platform
{
private:
float y;
float xi;
float xf;
public:
Platform(float y, float xi, float xf);
virtual ~Platform(void);
float getxi();
float getxf();
};
而且我希望能够做到这一点:
Platform* p = new Platform(1.0,2.0,3.0);
cout << p; // should it be *p?
我试图超载 “< <” 运营商,像这样:
// in Platform.cpp
std::ostream& operator<<(std::ostream& out, Platform* p)
{
out << "Platform: xi=" << p->getxi() << ", xf=" << p->getxf() << std::endl;
return out;
}
但这只是打印出内存地址(当然,因为p
是指针......)。 我很确定上述函数根本没有被调用。
*“否则,模板搜索并找到指针打印功能。” *不,他的过载,应改为调用。我猜,从Yakk说的那个地方他不知道从哪里来。 – Pubby
是的,我的错误。我没有注意到*和第一次阅读它... –
我应该在调用'cout << * p'的文件中添加'#include“Platform.cpp”'吗? –