c++ - 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
}
解决方案
我的问题是尝试使用带有地址的流插入的指针对象,在更改流插入运算符以获取指针之后,一切正常。
推荐阅读
- elasticsearch - Elasticsearch 检索 multi_match 中的匹配字段
- r - R - ggplot 自定义图例
- java - 通过反射将值设置为java类?
- c++ - 您将如何使用标准引号解释指向基类和派生类成员的指针的这种差异?
- bash - Anaconda 环境 bash 前缀太长
- python - 查找日志文件中的模式计数驻留在 zip 文件中而不提取
- sql - 如何使用 SQL 找到每个组的最小值和最大值?
- php - ES256 JWT 在 PHP 中为 Apple AppStoreConenct API 身份验证签名
- java - 了解 JBoss EAP 6.4 (HornetQ) 中的远程 JMS 连接
- sql-server - 在 Linux 中添加 SSL 证书后 SQL Server 服务中断