不能重载“<<”运算符

Can't overload "<<" operator

到处都看到每个人都在几行中重载了 << 运算符,这看起来很简单,但由于某些原因,在我的代码中重载了 << 运算符没有任何作用。

在我的 .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正在访问指针,而不是对象。