在我所看到的所有地方,我看到每个人都在几行中重载<<操作符,这似乎很简单,但是由于某种原因,在我的代码中重载<<操作符什么也没做。
在我的.h中,我有:
friend std::ostream& operator<<(std::ostream& os, const Test& test);
在我的.cpp文件中,我有:
std::ostream& operator<<(std::ostream& out,const DeckOfCards& deck) {
out << "oi"; //just testing with a normal string before i try methods
return out;
}
最后,在主要功能中,我有:
Test* test = new Test();
std::cout << "output is: " << test << std::endl;
有人可以告诉我我在做什么错吗?提前致谢。
尝试一下:
std::cout << "output is: " << *test << std::endl;
在代码中,您正在cout
查找指针,而不是对象。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句