首页 > 解决方案 > C ++流插入运算符返回位而不是字符串

问题描述

我的问题是,当我尝试通过 cout 使用它而不是来自输出 << 行的多行描述性字符串时,我的流插入运算符函数返回一个类似“0x55d55d9a6eb0”的字符串,这是相关代码。对于上下文,它是一个模板类,它有一个流插入运算符试图打印出一个多行字符串。

template <class V>
//friend ostream& operator<<(ostream&, const Report<V>&);
//CORRECTED VERSION:
friend ostream& operator<<(ostream&, const Report<V>*);
...
template <class V>
ostream& operator<<(ostream& output, const Report<V>& b)
{
  for(int i = 0;i<b.rows.size();i++){
    output << b.rows[i].row <<endl;// b.rows[i].row is a 1 line string
  }
  return output;
}
...
//how I am using the stream insertion operator in a seperate function:

void Main::print(Report<int> *r) {
    cout << r <<endl;//I have tried changing to &r, *r but no luck
}

标签: c++io

解决方案


我的问题是尝试使用带有地址的流插入的指针对象,在更改流插入运算符以获取指针之后,一切正常。


推荐阅读