std::ios_base::ios_base(const std::ios_base&)' is private 错误

本文介绍了C++中标准流操作符的正确使用方法,特别是如何避免因流对象复制而导致的错误,并展示了如何正确地通过引用传递istream和ostream对象。此外,还提供了使用ifstream进行文件读取的具体实例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

代码如下:

inline ostream operator<<(ostream& os, const istring s)
{
    return os<<string(s.c_str(), s.length());
}

 

编译它出现:std::ios_base::ios_base(const std::ios_base&)' is private 错误。

这是因为流作为参数传递时必须通过引用传递,所以正确的代码是

 

inline ostream& operator<<(ostream& os, const istring s)
{
    return os<<string(s.c_str(), s.length());

}


用std::ifstream,std::ofstream作为函数参数传递时,必须通过引用传递,因为其copy方法被私有化,从而保证对象的唯一性。

正确代码


  3 #include <iostream>
  4 #include <fstream>
  5 using namespace std;
  6
  7 OrderEntryMsg ReadOrderEntryMsg(ifstream &infile)
  8 {
  9     OrderEntryMsg msg;
 10     infile.seekg(sizeof(MsgHeader), ios::cur);
 11     infile.read((char*)&msg, sizeof(OrderEntryMsg));
 12     return msg;
 13 }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

错误代码

  7 OrderEntryMsg ReadOrderEntryMsg(ifstream infile)
  8 {
  9     OrderEntryMsg msg;
 10     infile.seekg(sizeof(MsgHeader), ios::cur);
 11     infile.read((char*)&msg, sizeof(OrderEntryMsg));
 12     return msg;
 13 }

#include <iostream> int main() { std::cout << "Hello MinGW!" << std::endl; return 0; }正在启动生成... cmd /c chcp 65001>nul && D:\gongzuo\minGW\mingw64\bin\gcc.exe -fdiagnostics-color=always -g D:\gongzuo\c++study\study1.cc -o D:\gongzuo\c++study\study1.exe C:\Users\hcl\AppData\Local\Temp\ccs5wWxN.o: In function `main': D:/gongzuo/c++study/study1.cc:3: undefined reference to `std::basic_ostream&lt;char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream&lt;char, std::char_traits<char> >&, char const*)' D:/gongzuo/c++study/study1.cc:3: undefined reference to `std::ostream::operator<<(std::ostream&amp; (*)(std::ostream&amp;))' C:\Users\hcl\AppData\Local\Temp\ccs5wWxN.o: In function `__tcf_0': D:/gongzuo/minGW/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/iostream:74: undefined reference to `std::ios_base::Init::~Init()' C:\Users\hcl\AppData\Local\Temp\ccs5wWxN.o: In function `__static_initialization_and_destruction_0': D:/gongzuo/minGW/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/iostream:74: undefined reference to `std::ios_base::Init::Init()' C:\Users\hcl\AppData\Local\Temp\ccs5wWxN.o:study1.cc:(.rdata$.refptr._ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_[.refptr._ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_]+0x0): undefined reference to `std::basic_ostream&lt;char, std::char_traits<char> >& std::endl<char, std::char_traits<char> >(std::basic_ostream&lt;char, std::char_traits<char> >&)' C:\Users\hcl\AppData\Local\Temp\ccs5wWxN.o:study1.cc:(.rdata$.refptr._ZSt4cout[.refptr._ZSt4cout]+0x0): undefined reference to `std::cout' collect2.exe: error: ld returned 1 exit status
07-23
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值