I/O: std::basic_streambuf

本文详细介绍了C++标准库中的stream buffer(streambuf),包括其内部结构、构造函数、成员函数如setp、sputc、sputn等,并提供了两个示例演示如何使用自定义的StreamBuffer类。

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

template class std::basic_streambuf控制输入,输出字符序列.

之前几篇博客介绍各个stream并不负责实际的读写,而是委托给stream buffer完成.

处理stream buffer的一般性接口十分简单:

stream::rdbuf() 取得一个buffer的指针指向某个stream的buffer.

stream::rdbuf(buffer* ptr) 给某个stream设置一个buffer,并返回之前的buffer的指针.

 

stream buffer即负责Input,也负责Output.

因此在buffer内其实可以想象成2个数组一个负责Input(read缓冲区中的内容),一个负责Output(write入缓冲区).

Output缓冲区(向stream的buffer中写入数据):

output缓冲区由3个pointer维护,这三个pointer分别由函数pbase(),pptr()和epptr()取得

1,pbase()返回output stream缓冲区的开始.

2,pptr()获得一个指向当前output stream buffer 可以写入的位置.

3,epptr()返回的是output缓冲区的尾端,类似尾后迭代器.

 

Input缓冲区(从stream的buffer读取数据):

Input缓冲区也由3个pointer维护,这三个pointer可由eback(),gptr(),egptr().

1,eback()返回input缓冲区的起点.

2,gptr()返回一个指向当前input stream buffer 可以读取的位置.

3,egptr()返回的是input缓冲区的尾端,类似尾后迭代器.

 

Constructor:

protected:
basic_streambuf();
 	
protected:
basic_streambuf(const basic_streambuf& rhs);

1, 默认构造函数为protected,意味着只有继承了该class的其他class才能使用.

2,拷贝构造函数也为protected,意味着只有继承了该class的其他class才能使用.

 

protected:

std::basic_streambuf::operator=

basic_streambuf& operator=( const basic_streambuf& other );
	

protected的拷贝复制运算符,同样也是只有继承了该class的其他class才能使用.

 

Output stream所使用的buffer接口:

std::basic_streambuf::setp

void setp( char_type* pbeg, char_type* pend );
	

前面我们介绍了pbase(), pptr(), epptr()函数用来返回指向缓冲区pointer,这些指针的开始位置,尾后位置都是通过该函数设置的:

pbeg:用来设置指向buffer开始的位置的指针.

pend: 用来设置指向buffer尾后位置的指针.

 

std::basic_streambuf::sputc

int_type sputc( char_type ch );
	

将字符ch拷贝进去缓冲区.

 

std::basic_streambuf::sputn

std::streamsize sputn( const char_type* s, std::streamsize count );
		
protected:
virtual std::streamsize xsputn( const char_type* s, std::streamsize count );

将字符序列s中的前count个字符拷贝到缓冲区,一般情况下是对std::basic_streambuf::sputc()调用count次,因此我们可以通过优化std::basic_streambuf::sputc来优化.

 

std::basic_streambuf::overflow

virtual int_type overflow( int_type ch = traits::eof() );
	

该函数是一个很特别的函数,根据标准由2种实现:

1,用来判断当前buffer区是否还有足够的空间存放字符,如果有足够的空间则拷贝字符进去buffer,否则返回 typename std::char_traits<charT>::eof();

2,仅仅用来返回typename std::char_traits<charT>::eof();缓存区空间的大小留给std::basic_streambuf::sputc()或者是std::basic_streambuf::xsputn()来判断.

 

Demo 1(未重写overflow):

#include <iostream>     // std::cin, std::cout
#include <string>
#include <streambuf>
#include <array>

template<typename charT, typename traits = std::char_traits<charT>>
class StreamBuffer : public std::basic_streambuf<charT> {
public:

	using char_type = typename traits::char_type;

	StreamBuffer();
	~StreamBuffer() = default;

	StreamBuffer(const StreamBuffer<charT>& other) = delete;
	StreamBuffer& operator=(const StreamBuffer<charT>& other) = delete;

	void printBuffer()const;

private:
	std::array<char_type, 10> buffer;
};

template<typename charT, typename tratis>
StreamBuffer<charT, tratis>::StreamBuffer()
{
	//注意这里这里设置的是缓冲区的开始位置和最末位置
	//可以通过pbase(), pptr(), epptr()来进行访问.
	this->std::basic_streambuf<charT>::setp((this->buffer).data(), (this->buffer).data()+9);
}

template<typename charT, typename traits>
void StreamBuffer<charT, traits>::printBuffer()const
{
	for (const char_type& value : (this->buffer)) {
		std::cout << value << " ";
	}
	std::cout << std::endl;
}


int main()
{
	StreamBuffer<char> buffer;
	std::basic_ostream<char> ostream(&buffer);

	ostream << "shihua";

	buffer.printBuffer();

	return 0;
}

 

Demo 2(重写overflow):

#include <iostream>
#include <streambuf>
#include <array>
#include <locale>

template<typename charT, typename traits = std::char_traits<charT>>
class StreamBuffer :public std::basic_streambuf<charT>
{
public:

	using charType = typename traits::char_type;
	using intType = typename traits::int_type;

	StreamBuffer();
	virtual ~StreamBuffer() = default;

	StreamBuffer(const StreamBuffer<charT>& other) = delete;
	StreamBuffer& operator=(const StreamBuffer<charT>& other) = delete;

	void printBuffer()const;

protected:
	virtual intType overflow(intType ch)override;
	virtual std::streamsize xsputn(const charT* s, std::streamsize count)override;

private:
	std::array<charType, 10> buffer;
	unsigned long counter;
};


template<typename charT, typename traits>
StreamBuffer<charT, traits>::StreamBuffer()
	:std::basic_streambuf<charT>(),
	 counter(buffer.size())
{
	this->std::basic_streambuf<charT>::setp((this->buffer).data(), (this->buffer).data(), (this->buffer).data() + 10);
}

template<typename charT, typename traits>
void StreamBuffer<charT, traits>::printBuffer()const
{
	for (const charType& value : (this->buffer)) {
		if ( !typename traits::eq(value, traits::eof())) {
			std::cout << value << " ";
		}
	}

	std::cout << std::endl;
}

template<typename charT, typename traits>
typename StreamBuffer<charT, traits>::intType StreamBuffer<charT, traits>::overflow(typename StreamBuffer<charT, traits>::intType ch)
{
	return (typename traits::eof());
}

template<typename charT, typename traits>
std::streamsize StreamBuffer<charT, traits>::xsputn(const charT* s, std::streamsize count)
{
	if (count > 0) {
		if (count < (this->buffer).size()) {
			typename traits::copy(this->pptr(), s, count);
			return count;

		}else {
			return (this->epptr() - this->pptr());
		}
	}
	return (this->overflow(*(this->pptr())));
}



int main()
{
	StreamBuffer<char> buffer;
	std::basic_ostream<char> cout(&buffer);

	cout << "shihua";

	buffer.printBuffer();

	return 0;
}

 

Input stream所用的buffer的接口:

std::basic_streambuf::in_avail

std::streamsize in_avail();
	

该函数返回当前buffer内还有多少有效字符未被读(read)出来.

 

std::basic_streambuf::uflow

virtual int_type uflow();
	

该函数的默认行为是: 调用std::basic_streambuf::underflow()并移动(前进)read pointer。但是一般情况下标准库对于std::basic_streambuf::underflow()的做法是令它返回EOF.

 

std::basic_streambuf::showmanyc

protected:
virtual std::streamsize showmanyc();
	

返回并估计当前缓冲区中还有多少个字符可以读.

其中需要注意的是 std::streamsize一般的实现是: typedef unsigned long long std::streamsize;

 

std::basic_streambuf::underflow

virtual int_type underflow();
	

默认被实现为返回EOF(typename std::char_traits<charType>::eof()).

 

std::basic_streambuf::sgetc

int_type sgetc();
	

读取一个字符(*gptr()),不会向前移动当前可读取位置.

 

std::basic_streambuf::sgetn

std::basic_streambuf::xsgetn

std::streamsize sgetn( char_type* s, std::streamsize count );

protected:
virtual std::streamsize xsgetn( char_type* s, std::streamsize count );

std::basic_streambuf::sgetn()其实是调用std::basic_streambuf::xsgetn().

从input stream buffer中读取count个字符存储到s指向的数组中去.

同时会调用std::basic_streambuf::gbump(count)使当前当前可读位置增加count.

 

std::basic_streambuf::gbump

void gbump( int count );
	

使得当前可读位置向前移动count个位置.

 

std::basic_streambuf::gset

void setg( char_type* pbeg, char_type* pend );
	

设置input stream buffer维护的3个pointer的初始化位置,该函数一般在构造函数中被调用.

eback() = pbeg; gptr()=pbeg; egptr()=pend;

 

Locales:

std::basic_streambuf::pubimbue

std::basic_streambuf::imbue

std::locale pubimbue( const std::locale& loc );
		
protected:
virtual void imbue( const std::locale& loc );

给当前stream buffer设置std::locale.

 

std::basic_streambuf::getloc

std::locale getloc() const;

返回当前std::basic_streambuf设置的std::locale.

 

Positioning:

std::basic_streambuf::pubsetbuf

std::basic_streambuf::setbuf

basic_streambuf<CharT, Traits>* pubsetbuf( char_type* s, std::streamsize n )
	
protected:
virtual basic_streambuf<CharT, Traits>* setbuf( char_type* s, std::streamsize n )

用我们给定的字符数组s的前n个作为缓冲区的存储介质.

 

std::basic_streambuf::pubseekoff

std::basic_streambuf::seekoff

pos_type pubseekoff( off_type off, ios_base::seekdir dir,
                     ios_base::openmode which = ios_base::in | ios_base::out );
	
protected:
virtual pos_type seekoff( off_type off, ios_base::seekdir dir,
                          ios_base::openmode which = ios_base::in | ios_base::out );

off:是偏移量.

dir:是定位到当前stream buffer的位置.

which:指定当前stream buffer的I/O方向.

通过dir定位到stream buffer的一个位置,然后通过offset指定的量偏移一定位置.

返回当前坐标位于buffer中的绝对位置.

 

std::basic_streambuf::pubseekpos

std::basic_streambuf::seekpos

pos_type pubseekpos( pos_type pos,
                     ios_base::openmode which = ios_base::in | ios_base::out );

protected:
virtual pos_type seekpos( pos_type pos,
                          ios_base::openmode which = ios_base::in | ios_base::out);

pos: 绝对位置.

which: I/O方向.

设置当前stream buffer中指向可读位置或者可写的位置到pos指定的绝对位置.

转载于:https://my.oschina.net/SHIHUAMarryMe/blog/748213

a.cpp: In function ‘int main(): a.cpp:47:17: error: ‘Swap’ was not declared in this scope 47 | Swap(&a, &b); | ^~~~ a.cpp:53:17: error: ‘Swap’ was not declared in this scope 53 | Swap(&a, &b); | ^~~~ a.cpp:59:17: error: ‘Swap’ was not declared in this scope 59 | Swap(&a, &b); | ^~~~ a.cpp:65:17: error: ‘Swap’ was not declared in this scope 65 | Swap(&a, &b); | ^~~~ a.cpp:71:17: error: ‘Swap’ was not declared in this scope 71 | Swap(&a, &b); | ^~~~ a.cpp:78:21: error: no match for ‘operator>>’ (operand types are ‘std::istream’ {aka ‘std::basic_istream<char>’} and ‘char*’) 78 | cin >> a >> b; | ~~~ ^~ ~ | | | | | char* | std::istream {aka std::basic_istream<char>} In file included from /usr/include/c++/11/iostream:40, from a.cpp:1: /usr/include/c++/11/istream:168:7: note: candidate:std::basic_istream<_CharT, _Traits>::__istream_type& std::basic_istream<_CharT, _Traits>::operator>>(bool&) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_istream<_CharT, _Traits>::__istream_type = std::basic_istream<char>]’ (near match) 168 | operator>>(bool& __n) | ^~~~~~~~ /usr/include/c++/11/istream:168:7: note: conversion of argument 1 would be ill-formed: a.cpp:78:24: error: cannot bind non-const lvalue reference of type ‘bool&’ to a value of type ‘char*’ 78 | cin >> a >> b; | ^ In file included from /usr/include/c++/11/iostream:40, from a.cpp:1: /usr/include/c++/11/istream:172:7: note: candidate:std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(short int&) [with _CharT = char; _Traits = std::char_traits<char>]’ (near match) 172 | operator>>(short& __n); | ^~~~~~~~ /usr/include/c++/11/istream:172:7: note: conversion of argument 1 would be ill-formed: a.cpp:78:24: error: invalid conversion from ‘char*’ to ‘short int’ [-fpermissive] 78 | cin >> a >> b; | ^ | | | char* a.cpp:78:24: error: cannot bind rvalue ‘(short int)a’ to ‘short int&’ In file included from /usr/include/c++/11/iostream:40, from a.cpp:1: /usr/include/c++/11/istream:175:7: note: candidate:std::basic_istream<_CharT, _Traits>::__istream_type& std::basic_istream<_CharT, _Traits>::operator>>(short unsigned int&) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_istream<_CharT, _Traits>::__istream_type = std::basic_istream<char>]’ (near match) 175 | operator>>(unsigned short& __n) | ^~~~~~~~ /usr/include/c++/11/istream:175:7: note: conversion of argument 1 would be ill-formed: a.cpp:78:24: error: invalid conversion from ‘char*’ to ‘short unsigned int’ [-fpermissive] 78 | cin >> a >> b; | ^ | | | char* a.cpp:78:24: error: cannot bind rvalue ‘(short unsigned int)a’ to ‘short unsigned int&’ In file included from /usr/include/c++/11/iostream:40, from a.cpp:1: /usr/include/c++/11/istream:179:7: note: candidate:std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(int&) [with _CharT = char; _Traits = std::char_traits<char>]’ (near match) 179 | operator>>(int& __n); | ^~~~~~~~ /usr/include/c++/11/istream:179:7: note: conversion of argument 1 would be ill-formed: a.cpp:78:24: error: invalid conversion from ‘char*’ to ‘int’ [-fpermissive] 78 | cin >> a >> b; | ^ | | | char* a.cpp:78:24: error: cannot bind rvalue ‘(int)a’ to ‘int&’ In file included from /usr/include/c++/11/iostream:40, from a.cpp:1: /usr/include/c++/11/istream:182:7: note: candidate:std::basic_istream<_CharT, _Traits>::__istream_type& std::basic_istream<_CharT, _Traits>::operator>>(unsigned int&) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_istream<_CharT, _Traits>::__istream_type = std::basic_istream<char>]’ (near match) 182 | operator>>(unsigned int& __n) | ^~~~~~~~ /usr/include/c++/11/istream:182:7: note: conversion of argument 1 would be ill-formed: a.cpp:78:24: error: invalid conversion from ‘char*’ to ‘unsigned int’ [-fpermissive] 78 | cin >> a >> b; | ^ | | | char* a.cpp:78:24: error: cannot bind rvalue ‘(unsigned int)a’ to ‘unsigned int&’ In file included from /usr/include/c++/11/iostream:40, from a.cpp:1: /usr/include/c++/11/istream:186:7: note: candidate:std::basic_istream<_CharT, _Traits>::__istream_type& std::basic_istream<_CharT, _Traits>::operator>>(long int&) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_istream<_CharT, _Traits>::__istream_type = std::basic_istream<char>]’ (near match) 186 | operator>>(long& __n) | ^~~~~~~~ /usr/include/c++/11/istream:186:7: note: conversion of argument 1 would be ill-formed: a.cpp:78:24: error: invalid conversion from ‘char*’ to ‘long int’ [-fpermissive] 78 | cin >> a >> b; | ^ | | | char* a.cpp:78:24: error: cannot bind rvalue ‘(long int)a’ to ‘long int&’ In file included from /usr/include/c++/11/iostream:40, from a.cpp:1: /usr/include/c++/11/istream:190:7: note: candidate:std::basic_istream<_CharT, _Traits>::__istream_type& std::basic_istream<_CharT, _Traits>::operator>>(long unsigned int&) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_istream<_CharT, _Traits>::__istream_type = std::basic_istream<char>]’ (near match) 190 | operator>>(unsigned long& __n) | ^~~~~~~~ /usr/include/c++/11/istream:190:7: note: conversion of argument 1 would be ill-formed: a.cpp:78:24: error: invalid conversion from ‘char*’ to ‘long unsigned int’ [-fpermissive] 78 | cin >> a >> b; | ^ | | | char* a.cpp:78:24: error: cannot bind rvalue ‘(long unsigned int)a’ to ‘long unsigned int&’ In file included from /usr/include/c++/11/iostream:40, from a.cpp:1: /usr/include/c++/11/istream:195:7: note: candidate:std::basic_istream<_CharT, _Traits>::__istream_type& std::basic_istream<_CharT, _Traits>::operator>>(long long int&) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_istream<_CharT, _Traits>::__istream_type = std::basic_istream<char>]’ (near match) 195 | operator>>(long long& __n) | ^~~~~~~~ /usr/include/c++/11/istream:195:7: note: conversion of argument 1 would be ill-formed: a.cpp:78:24: error: invalid conversion from ‘char*’ to ‘long long int’ [-fpermissive] 78 | cin >> a >> b; | ^ | | | char* a.cpp:78:24: error: cannot bind rvalue ‘(long long int)a’ to ‘long long int&’ In file included from /usr/include/c++/11/iostream:40, from a.cpp:1: /usr/include/c++/11/istream:199:7: note: candidate:std::basic_istream<_CharT, _Traits>::__istream_type& std::basic_istream<_CharT, _Traits>::operator>>(long long unsigned int&) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_istream<_CharT, _Traits>::__istream_type = std::basic_istream<char>]’ (near match) 199 | operator>>(unsigned long long& __n) | ^~~~~~~~ /usr/include/c++/11/istream:199:7: note: conversion of argument 1 would be ill-formed: a.cpp:78:24: error: invalid conversion from ‘char*’ to ‘long long unsigned int’ [-fpermissive] 78 | cin >> a >> b; | ^ | | | char* a.cpp:78:24: error: cannot bind rvalue ‘(long long unsigned int)a’ to ‘long long unsigned int&’ In file included from /usr/include/c++/11/iostream:40, from a.cpp:1: /usr/include/c++/11/istream:235:7: note: candidate:std::basic_istream<_CharT, _Traits>::__istream_type& std::basic_istream<_CharT, _Traits>::operator>>(void*&) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_istream<_CharT, _Traits>::__istream_type = std::basic_istream<char>]’ (near match) 235 | operator>>(void*& __p) | ^~~~~~~~ /usr/include/c++/11/istream:235:7: note: conversion of argument 1 would be ill-formed: a.cpp:78:24: error: cannot bind non-const lvalue reference of type ‘void*&’ to an rvalue of type ‘void*’ 78 | cin >> a >> b; | ^ In file included from /usr/include/c++/11/iostream:40, from a.cpp:1: /usr/include/c++/11/istream:120:7: note: candidate:std::basic_istream<_CharT, _Traits>::__istream_type& std::basic_istream<_CharT, _Traits>::operator>>(std::basic_istream<_CharT, _Traits>::__istream_type& (*)(std::basic_istream<_CharT, _Traits>::__istream_type&)) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_istream<_CharT, _Traits>::__istream_type = std::basic_istream<char>]’ 120 | operator>>(__istream_type& (*__pf)(__istream_type&)) | ^~~~~~~~ /usr/include/c++/11/istream:120:36: note: no known conversion for argument 1 from ‘char*’ to ‘std::basic_istream<char>::__istream_type& (*)(std::basic_istream<char>::__istream_type&)’ {aka ‘std::basic_istream<char>& (*)(std::basic_istream<char>&)’} 120 | operator>>(__istream_type& (*__pf)(__istream_type&)) | ~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~ /usr/include/c++/11/istream:124:7: note: candidate:std::basic_istream<_CharT, _Traits>::__istream_type& std::basic_istream<_CharT, _Traits>::operator>>(std::basic_istream<_CharT, _Traits>::__ios_type& (*)(std::basic_istream<_CharT, _Traits>::__ios_type&)) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_istream<_CharT, _Traits>::__istream_type = std::basic_istream<char>; std::basic_istream<_CharT, _Traits>::__ios_type = std::basic_ios<char>]’ 124 | operator>>(__ios_type& (*__pf)(__ios_type&)) | ^~~~~~~~ /usr/include/c++/11/istream:124:32: note: no known conversion for argument 1 from ‘char*’ to ‘std::basic_istream<char>::__ios_type& (*)(std::basic_istream<char>::__ios_type&)’ {aka ‘std::basic_ios<char>& (*)(std::basic_ios<char>&)’} 124 | operator>>(__ios_type& (*__pf)(__ios_type&)) | ~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~ /usr/include/c++/11/istream:131:7: note: candidate:std::basic_istream<_CharT, _Traits>::__istream_type& std::basic_istream<_CharT, _Traits>::operator>>(std::ios_base& (*)(std::ios_base&)) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_istream<_CharT, _Traits>::__istream_type = std::basic_istream<char>]’ 131 | operator>>(ios_base& (*__pf)(ios_base&)) | ^~~~~~~~ /usr/include/c++/11/istream:131:30: note: no known conversion for argument 1 from ‘char*’ to ‘std::ios_base& (*)(std::ios_base&)’ 131 | operator>>(ios_base& (*__pf)(ios_base&)) | ~~~~~~~~~~~~^~~~~~~~~~~~~~~~ /usr/include/c++/11/istream:214:7: note: candidate:std::basic_istream<_CharT, _Traits>::__istream_type& std::basic_istream<_CharT, _Traits>::operator>>(float&) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_istream<_CharT, _Traits>::__istream_type = std::basic_istream<char>]’ 214 | operator>>(float& __f) | ^~~~~~~~ /usr/include/c++/11/istream:214:25: note: no known conversion for argument 1 from ‘char*’ to ‘float&’ 214 | operator>>(float& __f) | ~~~~~~~^~~ /usr/include/c++/11/istream:218:7: note: candidate:std::basic_istream<_CharT, _Traits>::__istream_type& std::basic_istream<_CharT, _Traits>::operator>>(double&) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_istream<_CharT, _Traits>::__istream_type = std::basic_istream<char>]’ 218 | operator>>(double& __f) | ^~~~~~~~ /usr/include/c++/11/istream:218:26: note: no known conversion for argument 1 from ‘char*’ to ‘double&’ 218 | operator>>(double& __f) | ~~~~~~~~^~~ /usr/include/c++/11/istream:222:7: note: candidate:std::basic_istream<_CharT, _Traits>::__istream_type& std::basic_istream<_CharT, _Traits>::operator>>(long double&) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_istream<_CharT, _Traits>::__istream_type = std::basic_istream<char>]’ 222 | operator>>(long double& __f) | ^~~~~~~~ /usr/include/c++/11/istream:222:31: note: no known conversion for argument 1 from ‘char*’ to ‘long double&’ 222 | operator>>(long double& __f) | ~~~~~~~~~~~~~^~~ /usr/include/c++/11/istream:259:7: note: candidate:std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(std::basic_istream<_CharT, _Traits>::__streambuf_type*) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_istream<_CharT, _Traits>::__streambuf_type = std::basic_streambuf<char>]’ 259 | operator>>(__streambuf_type* __sb); | ^~~~~~~~ /usr/include/c++/11/istream:259:36: note: no known conversion for argument 1 from ‘char*’ to ‘std::basic_istream<char>::__streambuf_type*’ {aka ‘std::basic_streambuf<char>*’} 259 | operator>>(__streambuf_type* __sb); | ~~~~~~~~~~~~~~~~~~^~~~ In file included from /usr/include/c++/11/string:56, from /usr/include/c++/11/bits/locale_classes.h:40, from /usr/include/c++/11/bits/ios_base.h:41, from /usr/include/c++/11/ios:42, from /usr/include/c++/11/ostream:38, from /usr/include/c++/11/iostream:39, from a.cpp:1: /usr/include/c++/11/bits/basic_string.tcc:1485:5: note: candidate: ‘template<class _CharT, class _Traits, class _Alloc> std::basic_istream<_CharT, _Traits>& std::operator>>(std::basic_istream<_CharT, _Traits>&, std::__cxx11::basic_string<_CharT, _Traits, _Allocator>&)’ 1485 | operator>>(basic_istream<_CharT, _Traits>& __in, | ^~~~~~~~ /usr/include/c++/11/bits/basic_string.tcc:1485:5: note: template argument deduction/substitution failed: a.cpp:78:24: note: mismatched types ‘std::__cxx11::basic_string<_CharT, _Traits, _Allocator>’ and ‘char*’ 78 | cin >> a >> b; | ^ In file included from /usr/include/c++/11/istream:1016, from /usr/include/c++/11/iostream:40, from a.cpp:1: /usr/include/c++/11/bits/istream.tcc:958:5: note: candidate: ‘template<class _CharT, class _Traits> std::basic_istream<_CharT, _Traits>& std::operator>>(std::basic_istream<_CharT, _Traits>&, _CharT&)’ 958 | operator>>(basic_istream<_CharT, _Traits>& __in, _CharT& __c) | ^~~~~~~~ /usr/include/c++/11/bits/istream.tcc:958:5: note: template argument deduction/substitution failed: a.cpp:78:24: note: deduced conflicting types for parameter ‘_CharT’ (‘char’ and ‘char*’) 78 | cin >> a >> b; | ^ In file included from /usr/include/c++/11/iostream:40, from a.cpp:1: /usr/include/c++/11/istream:756:5: note: candidate: ‘template<class _Traits> std::basic_istream<char, _Traits>& std::operator>>(std::basic_istream<char, _Traits>&, unsigned char&)’ 756 | operator>>(basic_istream<char, _Traits>& __in, unsigned char& __c) | ^~~~~~~~ /usr/include/c++/11/istream:756:5: note: template argument deduction/substitution failed: a.cpp:78:24: note: cannot convert ‘a’ (type ‘char*’) to type ‘unsigned char&’ 78 | cin >> a >> b; | ^ In file included from /usr/include/c++/11/iostream:40, from a.cpp:1: /usr/include/c++/11/istream:761:5: note: candidate: ‘template<class _Traits> std::basic_istream<char, _Traits>& std::operator>>(std::basic_istream<char, _Traits>&, signed char&)’ 761 | operator>>(basic_istream<char, _Traits>& __in, signed char& __c) | ^~~~~~~~ /usr/include/c++/11/istream:761:5: note: template argument deduction/substitution failed: a.cpp:78:24: note: cannot convert ‘a’ (type ‘char*’) to type ‘signed char&’ 78 | cin >> a >> b; | ^ In file included from /usr/include/c++/11/iostream:40, from a.cpp:1: /usr/include/c++/11/istream:859:5: note: candidate: ‘template<class _CharT, class _Traits, long unsigned int _Num> std::basic_istream<_CharT, _Traits>& std::operator>>(std::basic_istream<_CharT, _Traits>&, _CharT (&)[_Num])’ 859 | operator>>(basic_istream<_CharT, _Traits>& __in, _CharT (&__s)[_Num]) | ^~~~~~~~ /usr/include/c++/11/istream:859:5: note: template argument deduction/substitution failed: a.cpp:78:24: note: mismatched types ‘_CharT [_Num]’ and ‘char*’ 78 | cin >> a >> b; | ^ In file included from /usr/include/c++/11/iostream:40, from a.cpp:1: /usr/include/c++/11/istream:868:5: note: candidate: ‘template<class _Traits, long unsigned int _Num> std::basic_istream<char, _Traits>& std::operator>>(std::basic_istream<char, _Traits>&, unsigned char (&)[_Num])’ 868 | operator>>(basic_istream<char, _Traits>& __in, unsigned char (&__s)[_Num]) | ^~~~~~~~ /usr/include/c++/11/istream:868:5: note: template argument deduction/substitution failed: a.cpp:78:24: note: mismatched types ‘unsigned char [_Num]’ and ‘char*’ 78 | cin >> a >> b; | ^ In file included from /usr/include/c++/11/iostream:40, from a.cpp:1: /usr/include/c++/11/istream:873:5: note: candidate: ‘template<class _Traits, long unsigned int _Num> std::basic_istream<char, _Traits>& std::operator>>(std::basic_istream<char, _Traits>&, signed char (&)[_Num])’ 873 | operator>>(basic_istream<char, _Traits>& __in, signed char (&__s)[_Num]) | ^~~~~~~~ /usr/include/c++/11/istream:873:5: note: template argument deduction/substitution failed: a.cpp:78:24: note: mismatched types ‘signed char [_Num]’ and ‘char*’ 78 | cin >> a >> b; | ^ In file included from /usr/include/c++/11/iostream:40, from a.cpp:1: /usr/include/c++/11/istream:1006:5: note: candidate: ‘template<class _Istream, class _Tp> _Istream&& std::operator>>(_Istream&&, _Tp&&)’ 1006 | operator>>(_Istream&& __is, _Tp&& __x) | ^~~~~~~~ /usr/include/c++/11/istream:1006:5: note: template argument deduction/substitution failed: /usr/include/c++/11/istream: In substitution of ‘template<class _Istream, class _Tp> _Istream&& std::operator>>(_Istream&&, _Tp&&) [with _Istream = std::basic_istream<char>&; _Tp = char*&]’: a.cpp:78:10: required from here /usr/include/c++/11/istream:1006:5: error: template constraint failure for ‘template<class _Is, class _Tp> requires (__derived_from_ios_base<_Is>) && requires(_Is& __is, _Tp&& __t) {__is >> (forward<_Tp>)(__t);} using __rvalue_stream_extraction_t = _Is&&’ /usr/include/c++/11/istream:1006:5: note: constraints not satisfied In file included from /usr/include/c++/11/iostream:39, from a.cpp:1: /usr/include/c++/11/ostream: In substitution of ‘template<class _Is, class _Tp> requires (__derived_from_ios_base<_Is>) && requires(_Is& __is, _Tp&& __t) {__is >> (forward<_Tp>)(__t);} using __rvalue_stream_extraction_t = _Is&& [with _Is = std::basic_istream<char>&; _Tp = char*&]’: /usr/include/c++/11/istream:1006:5: required by substitution of ‘template<class _Istream, class _Tp> _Istream&& std::operator>>(_Istream&&, _Tp&&) [with _Istream = std::basic_istream<char>&; _Tp = char*&]’ a.cpp:78:10: required from here /usr/include/c++/11/ostream:717:13: required for the satisfaction of ‘__derived_from_ios_base<_Is>’ [with _Is = std::basic_istream<char, std::char_traits<char> >&] /usr/include/c++/11/ostream:717:39: note: the expression ‘is_class_v<_Tp> [with _Tp = std::basic_istream<char, std::char_traits<char> >&]’ evaluated to ‘false’ 717 | concept __derived_from_ios_base = is_class_v<_Tp> | ^~~~~~~~~~~~~~~ a.cpp:79:17: error: ‘Swap’ was not declared in this scope 79 | Swap(a, b); | ^~~~ a.cpp:42:19: warning: unused variable ‘n’ [-Wunused-variable] 42 | int type, n, i; | ^ a.cpp:42:22: warning: unused variable ‘i’ [-Wunused-variable] 42 | int type, n, i; | ^ 上一个问题的代码出现了上述报错,请修改
最新发布
05-29
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值