std::istream::get的误用

今天想从流里面读数据到内存块里,写出如下代码:pStream->get(ba.data(), nCount),结果发现内存块里的数据错误,从某个字节以后都未赋值。仔细阅读了get函数的说明,明白错误出在了下面的第2条上。因为我读取的数据里有一个回车字符,用get函数会导致它自动截断。如果要从一个流读取内存块,正确的做法是用read方法。





std::istream::get

single character (1)
int get();
istream& get (char& c);
c-string (2)
istream& get (char* s, streamsize n);
istream& get (char* s, streamsize n, char delim);
stream buffer (3)
istream& get (streambuf& sb);
istream& get (streambuf& sb, char delim);
Get characters
Extracts characters from the stream, as  unformatted input:

(1) single character
Extracts a single character from the stream.
The character is either returned (first signature), or set as the value of its argument (second signature).
(2) c-string
Extracts characters from the stream and stores them in  s as a c-string, until either  (n-1) characters have been extracted or the  delimiting character is encountered: the  delimiting character being either the  newline character ( '\n') or  delim (if this argument is specified).
The  delimiting character is  not extracted from the input sequence if found, and remains there as the next character to be extracted from the stream (see  getline for an alternative that  does discard the  delimiting character).
null character ( '\0') is automatically appended to the written sequence if  n is greater than zero, even if an empty string is extracted.
(3) stream buffer
Extracts characters from the stream and inserts them into the output sequence controlled by the  stream buffer object  sb, stopping either as soon as such an insertion fails or as soon as the  delimiting character is encountered in the input sequence (the  delimiting character being either the newline character,  '\n', or  delim, if this argument is specified).
Only the characters successfully inserted into  sb are extracted from the stream: Neither the  delimiting character, nor eventually the character that failed to be inserted at  sb, are extracted from the input sequence and remain there as the next character to be extracted from the stream.
a.cpp: In function ‘int main(): a.cpp:7:16: error: no match for ‘operator>>’ (operand types are ‘std::basic_istream<char>::__istream_type’ {aka ‘std::basic_istream<char>’} and ‘<unresolved overloaded function type>’) 7 | cin>>number>>endl; | ~~~~~~~~~~~^~~~~~ In file included from /usr/include/c++/11/sstream:38, from /usr/include/c++/11/complex:45, from /usr/include/c++/11/ccomplex:39, from /usr/include/x86_64-linux-gnu/c++/11/bits/stdc++.h:54, 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 ‘<unresolved overloaded function type>’ 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 ‘<unresolved overloaded function type>’ 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 ‘<unresolved overloaded function type>’ to ‘std::ios_base& (*)(std::ios_base&)’ 131 | operator>>(ios_base& (*__pf)(ios_base&)) | ~~~~~~~~~~~~^~~~~~~~~~~~~~~~ /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>]’ 168 | operator>>(bool& __n) | ^~~~~~~~ /usr/include/c++/11/istream:168:24: note: no known conversion for argument 1 from ‘<unresolved overloaded function type>’ to ‘bool&’ 168 | operator>>(bool& __n) | ~~~~~~^~~ /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>]’ 172 | operator>>(short& __n); | ^~~~~~~~ /usr/include/c++/11/istream:172:25: note: no known conversion for argument 1 from ‘<unresolved overloaded function type>’ to ‘short int&’ 172 | operator>>(short& __n); | ~~~~~~~^~~ /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>]’ 175 | operator>>(unsigned short& __n) | ^~~~~~~~ /usr/include/c++/11/istream:175:34: note: no known conversion for argument 1 from ‘<unresolved overloaded function type>’ to ‘short unsigned int&’ 175 | operator>>(unsigned short& __n) | ~~~~~~~~~~~~~~~~^~~ /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>]’ 179 | operator>>(int& __n); | ^~~~~~~~ /usr/include/c++/11/istream:179:23: note: no known conversion for argument 1 from ‘<unresolved overloaded function type>’ to ‘int&’ 179 | operator>>(int& __n); | ~~~~~^~~ /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>]’ 182 | operator>>(unsigned int& __n) | ^~~~~~~~ /usr/include/c++/11/istream:182:32: note: no known conversion for argument 1 from ‘<unresolved overloaded function type>’ to ‘unsigned int&’ 182 | operator>>(unsigned int& __n) | ~~~~~~~~~~~~~~^~~ /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>]’ 186 | operator>>(long& __n) | ^~~~~~~~ /usr/include/c++/11/istream:186:24: note: no known conversion for argument 1 from ‘<unresolved overloaded function type>’ to ‘long int&’ 186 | operator>>(long& __n) | ~~~~~~^~~ /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>]’ 190 | operator>>(unsigned long& __n) | ^~~~~~~~ /usr/include/c++/11/istream:190:33: note: no known conversion for argument 1 from ‘<unresolved overloaded function type>’ to ‘long unsigned int&’ 190 | operator>>(unsigned long& __n) | ~~~~~~~~~~~~~~~^~~ /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>]’ 195 | operator>>(long long& __n) | ^~~~~~~~ /usr/include/c++/11/istream:195:29: note: no known conversion for argument 1 from ‘<unresolved overloaded function type>’ to ‘long long int&’ 195 | operator>>(long long& __n) | ~~~~~~~~~~~^~~ /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>]’ 199 | operator>>(unsigned long long& __n) | ^~~~~~~~ /usr/include/c++/11/istream:199:38: note: no known conversion for argument 1 from ‘<unresolved overloaded function type>’ to ‘long long unsigned int&’ 199 | operator>>(unsigned long long& __n) | ~~~~~~~~~~~~~~~~~~~~^~~ /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 ‘<unresolved overloaded function type>’ 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 ‘<unresolved overloaded function type>’ 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 ‘<unresolved overloaded function type>’ to ‘long double&’ 222 | operator>>(long double& __f) | ~~~~~~~~~~~~~^~~ /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>]’ 235 | operator>>(void*& __p) | ^~~~~~~~ /usr/include/c++/11/istream:235:25: note: no known conversion for argument 1 from ‘<unresolved overloaded function type>’ to ‘void*&’ 235 | operator>>(void*& __p) | ~~~~~~~^~~ /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 ‘<unresolved overloaded function type>’ to ‘std::basic_istream<char>::__streambuf_type*’ {aka ‘std::basic_streambuf<char>*’} 259 | operator>>(__streambuf_type* __sb); | ~~~~~~~~~~~~~~~~~~^~~~ In file included from /usr/include/x86_64-linux-gnu/c++/11/bits/stdc++.h:45, from a.cpp:1: /usr/include/c++/11/cstddef:132:5: note: candidate: ‘template<class _IntegerType> constexpr std::__byte_op_t<_IntegerType> std::operator>>(std::byte, _IntegerType)’ 132 | operator>>(byte __b, _IntegerType __shift) noexcept | ^~~~~~~~ /usr/include/c++/11/cstddef:132:5: note: template argument deduction/substitution failed: a.cpp:7:18: note: couldn’t deduce template parameter ‘_IntegerType’ 7 | cin>>number>>endl; | ^~~~ 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/istream:38, from /usr/include/c++/11/sstream:38, from /usr/include/c++/11/complex:45, from /usr/include/c++/11/ccomplex:39, from /usr/include/x86_64-linux-gnu/c++/11/bits/stdc++.h:54, 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:7:18: note: couldn’t deduce template parameter ‘_Alloc’ 7 | cin>>number>>endl; | ^~~~ In file included from /usr/include/c++/11/istream:1016, from /usr/include/c++/11/sstream:38, from /usr/include/c++/11/complex:45, from /usr/include/c++/11/ccomplex:39, from /usr/include/x86_64-linux-gnu/c++/11/bits/stdc++.h:54, from a.cpp:1: /usr/include/c++/11/bits/istream.tcc:958:5: note: candidate:std::basic_istream<_CharT, _Traits>& std::operator>>(std::basic_istream<_CharT, _Traits>&, _CharT&) [with _CharT = char; _Traits = std::char_traits<char>]’ 958 | operator>>(basic_istream<_CharT, _Traits>& __in, _CharT& __c) | ^~~~~~~~ /usr/include/c++/11/bits/istream.tcc:958:62: note: no known conversion for argument 2 from ‘<unresolved overloaded function type>’ to ‘char&’ 958 | operator>>(basic_istream<_CharT, _Traits>& __in, _CharT& __c) | ~~~~~~~~^~~ In file included from /usr/include/c++/11/sstream:38, from /usr/include/c++/11/complex:45, from /usr/include/c++/11/ccomplex:39, from /usr/include/x86_64-linux-gnu/c++/11/bits/stdc++.h:54, 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:7:18: note: cannot convert ‘std::endl’ (type ‘<unresolved overloaded function type>’) to type ‘unsigned char&’ 7 | cin>>number>>endl; | ^~~~ In file included from /usr/include/c++/11/sstream:38, from /usr/include/c++/11/complex:45, from /usr/include/c++/11/ccomplex:39, from /usr/include/x86_64-linux-gnu/c++/11/bits/stdc++.h:54, 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:7:18: note: cannot convert ‘std::endl’ (type ‘<unresolved overloaded function type>’) to type ‘signed char&’ 7 | cin>>number>>endl; | ^~~~ In file included from /usr/include/c++/11/sstream:38, from /usr/include/c++/11/complex:45, from /usr/include/c++/11/ccomplex:39, from /usr/include/x86_64-linux-gnu/c++/11/bits/stdc++.h:54, 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:7:18: note: couldn’t deduce template parameter ‘_Num’ 7 | cin>>number>>endl; | ^~~~ In file included from /usr/include/c++/11/sstream:38, from /usr/include/c++/11/complex:45, from /usr/include/c++/11/ccomplex:39, from /usr/include/x86_64-linux-gnu/c++/11/bits/stdc++.h:54, 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:7:18: note: couldn’t deduce template parameter ‘_Num’ 7 | cin>>number>>endl; | ^~~~ In file included from /usr/include/c++/11/sstream:38, from /usr/include/c++/11/complex:45, from /usr/include/c++/11/ccomplex:39, from /usr/include/x86_64-linux-gnu/c++/11/bits/stdc++.h:54, 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:7:18: note: couldn’t deduce template parameter ‘_Num’ 7 | cin>>number>>endl; | ^~~~ In file included from /usr/include/c++/11/sstream:38, from /usr/include/c++/11/complex:45, from /usr/include/c++/11/ccomplex:39, from /usr/include/x86_64-linux-gnu/c++/11/bits/stdc++.h:54, 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: a.cpp:7:18: note: couldn’t deduce template parameter ‘_Tp’ 7 | cin>>number>>endl; | ^~~~ In file included from /usr/include/c++/11/ccomplex:39, from /usr/include/x86_64-linux-gnu/c++/11/bits/stdc++.h:54, from a.cpp:1: /usr/include/c++/11/complex:501:5: note: candidate: ‘template<class _Tp, class _CharT, class _Traits> std::basic_istream<_CharT, _Traits>& std::operator>>(std::basic_istream<_CharT, _Traits>&, std::complex<_Tp>&)’ 501 | operator>>(basic_istream<_CharT, _Traits>& __is, complex<_Tp>& __x) | ^~~~~~~~ /usr/include/c++/11/complex:501:5: note: template argument deduction/substitution failed: a.cpp:7:18: note: couldn’t deduce template parameter ‘_Tp’ 7 | cin>>number>>endl; | ^~~~ In file included from /usr/include/x86_64-linux-gnu/c++/11/bits/stdc++.h:66, from a.cpp:1: /usr/include/c++/11/bitset:1472:5: note: candidate: ‘template<class _CharT, class _Traits, long unsigned int _Nb> std::basic_istream<_CharT, _Traits>& std::operator>>(std::basic_istream<_CharT, _Traits>&, std::bitset<_Nb>&)’ 1472 | operator>>(std::basic_istream<_CharT, _Traits>& __is, bitset<_Nb>& __x) | ^~~~~~~~ /usr/include/c++/11/bitset:1472:5: note: template argument deduction/substitution failed: a.cpp:7:18: note: couldn’t deduce template parameter ‘_Nb’ 7 | cin>>number>>endl; | ^~~~ In file included from /usr/include/x86_64-linux-gnu/c++/11/bits/stdc++.h:72, from a.cpp:1: /usr/include/c++/11/iomanip:71:5: note: candidate: ‘template<class _CharT, class _Traits> std::basic_istream<_CharT, _Traits>& std::operator>>(std::basic_istream<_CharT, _Traits>&, std::_Resetiosflags)’ 71 | operator>>(basic_istream<_CharT, _Traits>& __is, _Resetiosflags __f) | ^~~~~~~~ /usr/include/c++/11/iomanip:71:5: note: template argument deduction/substitution failed: a.cpp:7:18: note: cannot convert ‘std::endl’ (type ‘<unresolved overloaded function type>’) to type ‘std::_Resetiosflags’ 7 | cin>>number>>endl; | ^~~~ In file included from /usr/include/x86_64-linux-gnu/c++/11/bits/stdc++.h:72, from a.cpp:1: /usr/include/c++/11/iomanip:101:5: note: candidate: ‘template<class _CharT, class _Traits> std::basic_istream<_CharT, _Traits>& std::operator>>(std::basic_istream<_CharT, _Traits>&, std::_Setiosflags)’ 101 | operator>>(basic_istream<_CharT, _Traits>& __is, _Setiosflags __f) | ^~~~~~~~ /usr/include/c++/11/iomanip:101:5: note: template argument deduction/substitution failed: a.cpp:7:18: note: cannot convert ‘std::endl’ (type ‘<unresolved overloaded function type>’) to type ‘std::_Setiosflags’ 7 | cin>>number>>endl; | ^~~~ In file included from /usr/include/x86_64-linux-gnu/c++/11/bits/stdc++.h:72, from a.cpp:1: /usr/include/c++/11/iomanip:132:5: note: candidate: ‘template<class _CharT, class _Traits> std::basic_istream<_CharT, _Traits>& std::operator>>(std::basic_istream<_CharT, _Traits>&, std::_Setbase)’ 132 | operator>>(basic_istream<_CharT, _Traits>& __is, _Setbase __f) | ^~~~~~~~ /usr/include/c++/11/iomanip:132:5: note: template argument deduction/substitution failed: a.cpp:7:18: note: cannot convert ‘std::endl’ (type ‘<unresolved overloaded function type>’) to type ‘std::_Setbase’ 7 | cin>>number>>endl; | ^~~~ In file included from /usr/include/x86_64-linux-gnu/c++/11/bits/stdc++.h:72, from a.cpp:1: /usr/include/c++/11/iomanip:170:5: note: candidate:std::basic_istream<_CharT, _Traits>& std::operator>>(std::basic_istream<_CharT, _Traits>&, std::_Setfill<_CharT>) [with _CharT = char; _Traits = std::char_traits<char>]’ 170 | operator>>(basic_istream<_CharT, _Traits>& __is, _Setfill<_CharT> __f) | ^~~~~~~~ /usr/include/c++/11/iomanip:170:71: note: no known conversion for argument 2 from ‘<unresolved overloaded function type>’ to ‘std::_Setfill<char>’ 170 | operator>>(basic_istream<_CharT, _Traits>& __is, _Setfill<_CharT> __f) | ~~~~~~~~~~~~~~~~~^~~ /usr/include/c++/11/iomanip:200:5: note: candidate: ‘template<class _CharT, class _Traits> std::basic_istream<_CharT, _Traits>& std::operator>>(std::basic_istream<_CharT, _Traits>&, std::_Setprecision)’ 200 | operator>>(basic_istream<_CharT, _Traits>& __is, _Setprecision __f) | ^~~~~~~~ /usr/include/c++/11/iomanip:200:5: note: template argument deduction/substitution failed: a.cpp:7:18: note: cannot convert ‘std::endl’ (type ‘<unresolved overloaded function type>’) to type ‘std::_Setprecision’ 7 | cin>>number>>endl; | ^~~~ In file included from /usr/include/x86_64-linux-gnu/c++/11/bits/stdc++.h:72, from a.cpp:1: /usr/include/c++/11/iomanip:230:5: note: candidate: ‘template<class _CharT, class _Traits> std::basic_istream<_CharT, _Traits>& std::operator>>(std::basic_istream<_CharT, _Traits>&, std::_Setw)’ 230 | operator>>(basic_istream<_CharT, _Traits>& __is, _Setw __f) | ^~~~~~~~ /usr/include/c++/11/iomanip:230:5: note: template argument deduction/substitution failed: a.cpp:7:18: note: cannot convert ‘std::endl’ (type ‘<unresolved overloaded function type>’) to type ‘std::_Setw’ 7 | cin>>number>>endl; | ^~~~ In file included from /usr/include/x86_64-linux-gnu/c++/11/bits/stdc++.h:72, from a.cpp:1: /usr/include/c++/11/iomanip:264:5: note: candidate: ‘template<class _CharT, class _Traits, class _MoneyT> std::basic_istream<_CharT, _Traits>& std::operator>>(std::basic_istream<_CharT, _Traits>&, std::_Get_money<_MoneyT>)’ 264 | operator>>(basic_istream<_CharT, _Traits>& __is, _Get_money<_MoneyT> __f) | ^~~~~~~~ /usr/include/c++/11/iomanip:264:5: note: template argument deduction/substitution failed: a.cpp:7:18: note: couldn’t deduce template parameter ‘_MoneyT’ 7 | cin>>number>>endl; | ^~~~ In file included from /usr/include/x86_64-linux-gnu/c++/11/bits/stdc++.h:72, from a.cpp:1: /usr/include/c++/11/iomanip:418:5: note: candidate:std::basic_istream<_CharT, _Traits>& std::operator>>(std::basic_istream<_CharT, _Traits>&, std::_Get_time<_CharT>) [with _CharT = char; _Traits = std::char_traits<char>]’ 418 | operator>>(basic_istream<_CharT, _Traits>& __is, _Get_time<_CharT> __f) | ^~~~~~~~ /usr/include/c++/11/iomanip:418:72: note: no known conversion for argument 2 from ‘<unresolved overloaded function type>’ to ‘std::_Get_time<char>’ 418 | operator>>(basic_istream<_CharT, _Traits>& __is, _Get_time<_CharT> __f) | ~~~~~~~~~~~~~~~~~~^~~ In file included from /usr/include/c++/11/valarray:603, from /usr/include/x86_64-linux-gnu/c++/11/bits/stdc++.h:95, from a.cpp:1: /usr/include/c++/11/bits/valarray_after.h:414:5: note: candidate: ‘template<class _Dom1, class _Dom2> std::_Expr<std::__detail::_BinClos<std::__shift_right, std::_Expr, std::_Expr, _Dom1, _Dom2>, typename std::__fun<std::__shift_right, typename _Dom1::value_type>::result_type> std::operator>>(const std::_Expr<_Dom1, typename _Dom1::value_type>&, const std::_Expr<_Dom2, typename _Dom2::value_type>&)’ 414 | _DEFINE_EXPR_BINARY_OPERATOR(>>, struct std::__shift_right) | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~ /usr/include/c++/11/bits/valarray_after.h:414:5: note: template argument deduction/substitution failed: a.cpp:7:18: note:std::basic_istream<char>::__istream_type’ {aka ‘std::basic_istream<char>’} is not derived from ‘const std::_Expr<_Dom1, typename _Dom1::value_type>’ 7 | cin>>number>>endl; | ^~~~ In file included from /usr/include/c++/11/valarray:603, from /usr/include/x86_64-linux-gnu/c++/11/bits/stdc++.h:95, from a.cpp:1: /usr/include/c++/11/bits/valarray_after.h:414:5: note: candidate: ‘template<class _Dom> std::_Expr<std::__detail::_BinClos<std::__shift_right, std::_Expr, std::_Constant, _Dom, typename _Dom::value_type>, typename std::__fun<std::__shift_right, typename _Dom1::value_type>::result_type> std::operator>>(const std::_Expr<_Dom1, typename _Dom1::value_type>&, const typename _Dom::value_type&)’ 414 | _DEFINE_EXPR_BINARY_OPERATOR(>>, struct std::__shift_right) | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~ /usr/include/c++/11/bits/valarray_after.h:414:5: note: template argument deduction/substitution failed: a.cpp:7:18: note:std::basic_istream<char>::__istream_type’ {aka ‘std::basic_istream<char>’} is not derived from ‘const std::_Expr<_Dom1, typename _Dom1::value_type>’ 7 | cin>>number>>endl; | ^~~~ In file included from /usr/include/c++/11/valarray:603, from /usr/include/x86_64-linux-gnu/c++/11/bits/stdc++.h:95, from a.cpp:1: /usr/include/c++/11/bits/valarray_after.h:414:5: note: candidate: ‘template<class _Dom> std::_Expr<std::__detail::_BinClos<std::__shift_right, std::_Constant, std::_Expr, typename _Dom::value_type, _Dom>, typename std::__fun<std::__shift_right, typename _Dom1::value_type>::result_type> std::operator>>(const typename _Dom::value_type&, const std::_Expr<_Dom1, typename _Dom1::value_type>&)’ 414 | _DEFINE_EXPR_BINARY_OPERATOR(>>, struct std::__shift_right) | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~ /usr/include/c++/11/bits/valarray_after.h:414:5: note: template argument deduction/substitution failed: a.cpp:7:18: note: couldn’t deduce template parameter ‘_Dom’ 7 | cin>>number>>endl; | ^~~~ In file included from /usr/include/c++/11/valarray:603, from /usr/include/x86_64-linux-gnu/c++/11/bits/stdc++.h:95, from a.cpp:1: /usr/include/c++/11/bits/valarray_after.h:414:5: note: candidate: ‘template<class _Dom> std::_Expr<std::__detail::_BinClos<std::__shift_right, std::_Expr, std::_ValArray, _Dom, typename _Dom::value_type>, typename std::__fun<std::__shift_right, typename _Dom1::value_type>::result_type> std::operator>>(const std::_Expr<_Dom1, typename _Dom1::value_type>&, const std::valarray<typename _Dom::value_type>&)’ 414 | _DEFINE_EXPR_BINARY_OPERATOR(>>, struct std::__shift_right) | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~ /usr/include/c++/11/bits/valarray_after.h:414:5: note: template argument deduction/substitution failed: a.cpp:7:18: note:std::basic_istream<char>::__istream_type’ {aka ‘std::basic_istream<char>’} is not derived from ‘const std::_Expr<_Dom1, typename _Dom1::value_type>’ 7 | cin>>number>>endl; | ^~~~ In file included from /usr/include/c++/11/valarray:603, from /usr/include/x86_64-linux-gnu/c++/11/bits/stdc++.h:95, from a.cpp:1: /usr/include/c++/11/bits/valarray_after.h:414:5: note: candidate: ‘template<class _Dom> std::_Expr<std::__detail::_BinClos<std::__shift_right, std::_ValArray, std::_Expr, typename _Dom::value_type, _Dom>, typename std::__fun<std::__shift_right, typename _Dom1::value_type>::result_type> std::operator>>(const std::valarray<typename _Dom::value_type>&, const std::_Expr<_Dom1, typename _Dom1::value_type>&)’ 414 | _DEFINE_EXPR_BINARY_OPERATOR(>>, struct std::__shift_right) | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~ /usr/include/c++/11/bits/valarray_after.h:414:5: note: template argument deduction/substitution failed: a.cpp:7:18: note: couldn’t deduce template parameter ‘_Dom’ 7 | cin>>number>>endl; | ^~~~ In file included from /usr/include/x86_64-linux-gnu/c++/11/bits/stdc++.h:95, from a.cpp:1: /usr/include/c++/11/valarray:1195:1: note: candidate: ‘template<class _Tp> std::_Expr<std::__detail::_BinClos<std::__shift_right, std::_ValArray, std::_ValArray, _Tp, _Tp>, typename std::__fun<std::__shift_right, _Tp>::result_type> std::operator>>(const std::valarray<_Tp>&, const std::valarray<_Tp>&)’ 1195 | _DEFINE_BINARY_OPERATOR(>>, __shift_right) | ^~~~~~~~~~~~~~~~~~~~~~~ /usr/include/c++/11/valarray:1195:1: note: template argument deduction/substitution failed: a.cpp:7:18: note:std::basic_istream<char>::__istream_type’ {aka ‘std::basic_istream<char>’} is not derived from ‘const std::valarray<_Tp>’ 7 | cin>>number>>endl; | ^~~~ In file included from /usr/include/x86_64-linux-gnu/c++/11/bits/stdc++.h:95, from a.cpp:1: /usr/include/c++/11/valarray:1195:1: note: candidate: ‘template<class _Tp> std::_Expr<std::__detail::_BinClos<std::__shift_right, std::_ValArray, std::_Constant, _Tp, _Tp>, typename std::__fun<std::__shift_right, _Tp>::result_type> std::operator>>(const std::valarray<_Tp>&, const typename std::valarray<_Tp>::value_type&)’ 1195 | _DEFINE_BINARY_OPERATOR(>>, __shift_right) | ^~~~~~~~~~~~~~~~~~~~~~~ /usr/include/c++/11/valarray:1195:1: note: template argument deduction/substitution failed: a.cpp:7:18: note:std::basic_istream<char>::__istream_type’ {aka ‘std::basic_istream<char>’} is not derived from ‘const std::valarray<_Tp>’ 7 | cin>>number>>endl; | ^~~~ In file included from /usr/include/x86_64-linux-gnu/c++/11/bits/stdc++.h:95, from a.cpp:1: /usr/include/c++/11/valarray:1195:1: note: candidate: ‘template<class _Tp> std::_Expr<std::__detail::_BinClos<std::__shift_right, std::_Constant, std::_ValArray, _Tp, _Tp>, typename std::__fun<std::__shift_right, _Tp>::result_type> std::operator>>(const typename std::valarray<_Tp>::value_type&, const std::valarray<_Tp>&)’ 1195 | _DEFINE_BINARY_OPERATOR(>>, __shift_right) | ^~~~~~~~~~~~~~~~~~~~~~~ /usr/include/c++/11/valarray:1195:1: note: template argument deduction/substitution failed: a.cpp:7:18: note: couldn’t deduce template parameter ‘_Tp’ 7 | cin>>number>>endl; | ^~~~ In file included from /usr/include/c++/11/random:51, from /usr/include/x86_64-linux-gnu/c++/11/bits/stdc++.h:108, from a.cpp:1: /usr/include/c++/11/bits/random.tcc:921:5: note: candidate: ‘template<class _IntType, class _CharT, class _Traits> std::basic_istream<_CharT, _Traits>& std::operator>>(std::basic_istream<_CharT, _Traits>&, std::uniform_int_distribution<_IntType>&)’ 921 | operator>>(std::basic_istream<_CharT, _Traits>& __is, | ^~~~~~~~ /usr/include/c++/11/bits/random.tcc:921:5: note: template argument deduction/substitution failed: a.cpp:7:18: note: couldn’t deduce template parameter ‘_IntType’ 7 | cin>>number>>endl; | ^~~~ In file included from /usr/include/c++/11/random:51, from /usr/include/x86_64-linux-gnu/c++/11/bits/stdc++.h:108, from a.cpp:1: /usr/include/c++/11/bits/random.tcc:982:5: note: candidate: ‘template<class _RealType, class _CharT, class _Traits> std::basic_istream<_CharT, _Traits>& std::operator>>(std::basic_istream<_CharT, _Traits>&, std::uniform_real_distribution<_IntType>&)’ 982 | operator>>(std::basic_istream<_CharT, _Traits>& __is, | ^~~~~~~~ /usr/include/c++/11/bits/random.tcc:982:5: note: template argument deduction/substitution failed: a.cpp:7:18: note: couldn’t deduce template parameter ‘_RealType’ 7 | cin>>number>>endl; | ^~~~ In file included from /usr/include/c++/11/random:51, from /usr/include/x86_64-linux-gnu/c++/11/bits/stdc++.h:108, from a.cpp:1: /usr/include/c++/11/bits/random.tcc:2167:5: note: candidate: ‘template<class _RealType, class _CharT, class _Traits> std::basic_istream<_CharT, _Traits>& std::operator>>(std::basic_istream<_CharT, _Traits>&, std::cauchy_distribution<_RealType>&)’ 2167 | operator>>(std::basic_istream<_CharT, _Traits>& __is, | ^~~~~~~~ /usr/include/c++/11/bits/random.tcc:2167:5: note: template argument deduction/substitution failed: a.cpp:7:18: note: couldn’t deduce template parameter ‘_RealType’ 7 | cin>>number>>endl; | ^~~~ In file included from /usr/include/c++/11/random:49, from /usr/include/x86_64-linux-gnu/c++/11/bits/stdc++.h:108, from a.cpp:1: /usr/include/c++/11/bits/random.h:3722:5: note: candidate: ‘template<class _CharT, class _Traits> std::basic_istream<_CharT, _Traits>& std::operator>>(std::basic_istream<_CharT, _Traits>&, std::bernoulli_distribution&)’ 3722 | operator>>(std::basic_istream<_CharT, _Traits>& __is, | ^~~~~~~~ /usr/include/c++/11/bits/random.h:3722:5: note: template argument deduction/substitution failed: a.cpp:7:18: note: cannot convert ‘std::endl’ (type ‘<unresolved overloaded function type>’) to type ‘std::bernoulli_distribution&’ 7 | cin>>number>>endl; | ^~~~ In file included from /usr/include/c++/11/random:51, from /usr/include/x86_64-linux-gnu/c++/11/bits/stdc++.h:108, from a.cpp:1: /usr/include/c++/11/bits/random.tcc:1124:5: note: candidate: ‘template<class _IntType, class _CharT, class _Traits> std::basic_istream<_CharT, _Traits>& std::operator>>(std::basic_istream<_CharT, _Traits>&, std::geometric_distribution<_IntType>&)’ 1124 | operator>>(std::basic_istream<_CharT, _Traits>& __is, | ^~~~~~~~ /usr/include/c++/11/bits/random.tcc:1124:5: note: template argument deduction/substitution failed: a.cpp:7:18: note: couldn’t deduce template parameter ‘_IntType’ 7 | cin>>number>>endl; | ^~~~ In file included from /usr/include/c++/11/random:51, from /usr/include/x86_64-linux-gnu/c++/11/bits/stdc++.h:108, from a.cpp:1: /usr/include/c++/11/bits/random.tcc:1775:5: note: candidate: ‘template<class _RealType, class _CharT, class _Traits> std::basic_istream<_CharT, _Traits>& std::operator>>(std::basic_istream<_CharT, _Traits>&, std::exponential_distribution<_RealType>&)’ 1775 | operator>>(std::basic_istream<_CharT, _Traits>& __is, | ^~~~~~~~ /usr/include/c++/11/bits/random.tcc:1775:5: note: template argument deduction/substitution failed: a.cpp:7:18: note: couldn’t deduce template parameter ‘_RealType’ 7 | cin>>number>>endl; | ^~~~ In file included from /usr/include/c++/11/random:51, from /usr/include/x86_64-linux-gnu/c++/11/bits/stdc++.h:108, from a.cpp:1: /usr/include/c++/11/bits/random.tcc:2561:5: note: candidate: ‘template<class _RealType, class _CharT, class _Traits> std::basic_istream<_CharT, _Traits>& std::operator>>(std::basic_istream<_CharT, _Traits>&, std::weibull_distribution<_RealType>&)’ 2561 | operator>>(std::basic_istream<_CharT, _Traits>& __is, | ^~~~~~~~教我怎么看编译错误的原因
最新发布
10-20
#include <iostream> #include <string> #include <vector> #include "Vehicle.h" // 假设上述代码在Vehicle.h中 using namespace std; // 时间类,用于记录车辆进出时间 class Time { private: int hour, minute, second; public: Time(int h = 0, int m = 0, int s = 0) : hour(h), minute(m), second(s) {} // 获取时间差(分钟) int getTimeDifference(const Time& other) const; // 显示时间 void display() const; // 友元函数,用于输入时间 friend std::istream& operator>>(std::istream& is, Time& time); }; // 车辆基类(第一层) class Vehicle { protected: std::string plateNumber; // 车牌号 std::string type; // 车辆类型 Time entryTime; // 入场时间 int spotId; // 停车位ID bool isParked; // 是否在停车场内 public: Vehicle(const std::string& plate) : plateNumber(plate), isParked(false), spotId(-1) {} virtual ~Vehicle() {} // 虚函数1:获取停车费用 virtual double getParkingFee(int hours) const = 0; // 虚函数2:获取车辆类型 virtual std::string getVehicleType() const = 0; // 封装方法 void setEntryTime(const Time& time) { entryTime = time; } Time getEntryTime() const { return entryTime; } void setParked(bool status) { isParked = status; } bool isParkedStatus() const { return isParked; } void setSpotId(int id) { spotId = id; } int getSpotId() const { return spotId; } std::string getPlateNumber() const { return plateNumber; } // 友元函数,用于显示车辆信息 friend std::ostream& operator<<(std::ostream& os, const Vehicle& vehicle); }; // 第二层派生类 class Car : public Vehicle { private: int passengerCount; // 乘客数量 public: Car(const std::string& plate, int passengers = 1) : Vehicle(plate), passengerCount(passengers) { type = "Car"; } double getParkingFee(int hours) const override { return hours * 10.0; // 小车每小时10元 } std::string getVehicleType() const override { return "Car"; } // 封装方法 int getPassengerCount() const { return passengerCount; } void setPassengerCount(int count) { passengerCount = count; } }; class Truck : public Vehicle { private: double loadCapacity; // 载重能力(吨) public: Truck(const std::string& plate, double capacity) : Vehicle(plate), loadCapacity(capacity) { type = "Truck"; } double getParkingFee(int hours) const override { return hours * 20.0; // 卡车每小时20元 } std::string getVehicleType() const override { return "Truck"; } // 封装方法 double getLoadCapacity() const { return loadCapacity; } void setLoadCapacity(double capacity) { loadCapacity = capacity; } }; class Motorcycle : public Vehicle { private: bool hasSidecar; // 是否有边车 public: Motorcycle(const std::string& plate, bool sidecar = false) : Vehicle(plate), hasSidecar(sidecar) { type = "Motorcycle"; } double getParkingFee(int hours) const override { return hours * 5.0; // 摩托车每小时5元 } std::string getVehicleType() const override { return "Motorcycle"; } // 封装方法 bool hasSideCar() const { return hasSidecar; } void setSideCar(bool sidecar) { hasSidecar = sidecar; } }; // 停车位类 class ParkingSpot { private: int id; bool isOccupied; std::string vehicleType; // 适合的车辆类型 // 友元函数,允许ParkingLot类访问私有成员 friend class ParkingLot; public: ParkingSpot(int spotId, const std::string& type) : id(spotId), isOccupied(false), vehicleType(type) {} // 封装方法 int getId() const { return id; } bool isOccupiedStatus() const { return isOccupied; } std::string getVehicleType() const { return vehicleType; } }; // 停车场类(使用模板和vector) template <typename T> class ParkingLot { private: std::vector<ParkingSpot> spots; std::vector<T*> parkedVehicles; double totalIncome; int carCount, truckCount, motorcycleCount; // 查找可用停车位 int findAvailableSpot(const std::string& vehicleType); public: ParkingLot(int spotCount) : totalIncome(0.0), carCount(0), truckCount(0), motorcycleCount(0) { // 初始化停车位 for (int i = 0; i < spotCount; i++) { if (i < spotCount / 2) { spots.push_back(ParkingSpot(i, "Car")); // 前半部分为小车位 } else if (i < spotCount * 3 / 4) { spots.push_back(ParkingSpot(i, "Truck")); // 部分为卡车位 } else { spots.push_back(ParkingSpot(i, "Motorcycle")); // 部分为摩托车位 } } } ~ParkingLot() { for (T* vehicle : parkedVehicles) { delete vehicle; } } // 车辆入场 bool parkVehicle(T* vehicle, const Time& entryTime) { int spotId = findAvailableSpot(vehicle->getVehicleType()); if (spotId == -1) { std::cout << "停车场已满,无法停车!" << std::endl; return false; } vehicle->setEntryTime(entryTime); vehicle->setParked(true); vehicle->setSpotId(spotId); parkedVehicles.push_back(vehicle); // 更新停车位状态 spots[spotId].isOccupied = true; // 更新车辆统计 if (vehicle->getVehicleType() == "Car") { carCount++; } else if (vehicle->getVehicleType() == "Truck") { truckCount++; } else if (vehicle->getVehicleType() == "Motorcycle") { motorcycleCount++; } std::cout << "车辆 " << vehicle->getPlateNumber() << " 已停在车位 " << spotId << std::endl; return true; } // 车辆出场计费 double removeVehicle(const std::string& plateNumber, const Time& exitTime) { for (size_t i = 0; i < parkedVehicles.size(); i++) { if (parkedVehicles[i]->getPlateNumber() == plateNumber) { T* vehicle = parkedVehicles[i]; if (!vehicle->isParkedStatus()) { std::cout << "车辆 " << plateNumber << " 不在停车场内!" << std::endl; return 0.0; } // 计算停车时间(小时) int timeDifference = vehicle->getEntryTime().getTimeDifference(exitTime); int hours = (timeDifference + 59) / 60; // 向上取整到小时 // 计算费用 double fee = vehicle->getParkingFee(hours); totalIncome += fee; // 更新停车位状态 spots[vehicle->getSpotId()].isOccupied = false; // 更新车辆状态 vehicle->setParked(false); // 显示收费信息 std::cout << "车辆 " << plateNumber << " 离开停车场" << std::endl; std::cout << "停车时间: " << timeDifference << " 分钟" << std::endl; std::cout << "应付费用: " << fee << " 元" << std::endl; // 从列表中移除车辆 parkedVehicles.erase(parkedVehicles.begin() + i); // 更新车辆统计 if (vehicle->getVehicleType() == "Car") { carCount--; } else if (vehicle->getVehicleType() == "Truck") { truckCount--; } else if (vehicle->getVehicleType() == "Motorcycle") { motorcycleCount--; } return fee; } } std::cout << "未找到车辆 " << plateNumber << " 在停车场内!" << std::endl; return 0.0; } // 显示停车场状态 void displayStatus() const { std::cout << "\n===== 停车场状态 =====" << std::endl; std::cout << "总车位数: " << spots.size() << std::endl; std::cout << "已占用车位数: " << parkedVehicles.size() << std::endl; std::cout << "空闲车位数: " << spots.size() - parkedVehicles.size() << std::endl; std::cout << "\n各类型车辆数量:" << std::endl; std::cout << "小汽车: " << carCount << std::endl; std::cout << "卡车: " << truckCount << std::endl; std::cout << "摩托车: " << motorcycleCount << std::endl; std::cout << "总收入: " << totalIncome << " 元" << std::endl; std::cout << "小汽车数量: " << carCount << std::endl; std::cout << "卡车数量: " << truckCount << std::endl; std::cout << "摩托车数量: " << motorcycleCount << std::endl; std::cout << "=====================" << std::endl; } }; int main() { // 创建一个有20个车位的停车场 ParkingLot<Vehicle> parkingLot(20); Time currentTime; int choice; string plateNumber; while (true) { cout << "\n===== 停车场管理系统 =====" << endl; cout << "1. 车辆入场" << endl; cout << "2. 车辆出场" << endl; cout << "3. 查看停车场状态" << endl; cout << "4. 查看统计数据" << endl; cout << "5. 退出系统" << endl; cout << "请选择操作: "; cin >> choice; switch (choice) { case 1: { cout << "\n--- 车辆入场 ---" << endl; cout << "输入车牌号: "; cin >> plateNumber; cout << "选择车辆类型:" << endl; cout << "1. 小汽车" << endl; cout << "2. 卡车" << endl; cout << "3. 摩托车" << endl; int vehicleType; cin >> vehicleType; Vehicle* vehicle = nullptr; if (vehicleType == 1) { int passengers; cout << "输入乘客数量: "; cin >> passengers; vehicle = new Car(plateNumber, passengers); } else if (vehicleType == 2) { double capacity; cout << "输入载重能力(吨): "; cin >> capacity; vehicle = new Truck(plateNumber, capacity); } else if (vehicleType == 3) { bool sidecar; cout << "是否有边车(1-有, 0-无): "; cin >> sidecar; vehicle = new Motorcycle(plateNumber, sidecar); } else { cout << "无效的车辆类型!" << endl; break; } cout << "输入入场时间(时 分 秒): "; cin >> currentTime; if (vehicle) { parkingLot.parkVehicle(vehicle, currentTime); } break; } case 2: { cout << "\n--- 车辆出场 ---" << endl; cout << "输入车牌号: "; cin >> plateNumber; cout << "输入出场时间(时 分 秒): "; cin >> currentTime; parkingLot.removeVehicle(plateNumber, currentTime); break; } case 3: parkingLot.displayStatus(); break; case 4: parkingLot.getStatistics(); break; case 5: cout << "感谢使用停车场管理系统,再见!" << endl; return 0; default: cout << "无效的选择,请重新输入!" << endl; } } return 0; } 找出该程序的错误并解决
06-17
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值