【问题标题】:Can't overload "<<" operator不能重载“<<”运算符
【发布时间】:2016-09-07 23:40:27
【问题描述】:

我到处看到每个人都在几行中重载了

在我的 .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;

有人可以告诉我我做错了什么吗? 提前致谢。

【问题讨论】:

  • 不要不必要地使用new/delete。

标签: c++ output overloading


【解决方案1】:

试试这个怎么样:

std::cout << "output is: " << *test << std::endl;

在您的代码中,您是 couting 指针,而不是对象。

【讨论】:

  • 哇,我把 asterix 放在了测试之后而不是之前...感谢您的快速回复。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-12-07
  • 1970-01-01
  • 2015-11-13
  • 2021-02-17
  • 1970-01-01
  • 2013-07-30
  • 1970-01-01
相关资源
最近更新 更多