我是 C++ 新手,在理解它的行为时遇到了一些困难,因为它与我研究过的其他语言完全不同。
有人可以指出我正确的方向来修复此代码并解释错误日志吗?
我正在使用http://www.compileonline.com/compile_cpp_online.php来编译和运行这段代码。
声明一个数组(向量)变量的大小为 5,然后 while 循环内的 for 循环将填充它并更改值。在 while 的第一个循环中,for 循环填充它,如果输入的第一个值是 -1,则 while 循环应该停止。否则继续允许用户更改值。
#include <iostream>
#include<vector>
using namespace std;
int main()
{
vector<int> items(5);
do
{
cout << "Type customer details: " << endl;
for(int i = 0; i < 5; i++)
{
cout << "info " << 0 << ": ";
items[i] >> cin;
}
int newBalance = items[1] + items[2] - items[3];
if(newBalance > items[4])
{
cout << items[0] << " - " << items[4] << " - " << newBalance << " - Credit limit exceeded." << endl;
}
}while(items[0] != -1);
}
错误日志:
Compiling the source code....
$g++ main.cpp -o demo -lm -pthread -lgmpxx -lgmp -lreadline 2>&1
main.cpp: In function 'int main()':
main.cpp:18:13: error: no match for 'operator>>' (operand types are 'int' and 'std::istream {aka std::basic_istream<char>}')
items[i] >> cin;
^
main.cpp:18:13: note: candidates are:
In file included from /usr/local/gcc-4.8.1/include/c++/4.8.1/string:53:0,
from /usr/local/gcc-4.8.1/include/c++/4.8.1/bits/locale_classes.h:40,
from /usr/local/gcc-4.8.1/include/c++/4.8.1/bits/ios_base.h:41,
from /usr/local/gcc-4.8.1/include/c++/4.8.1/ios:42,
from /usr/local/gcc-4.8.1/include/c++/4.8.1/ostream:38,
from /usr/local/gcc-4.8.1/include/c++/4.8.1/iostream:39,
from main.cpp:1:
/usr/local/gcc-4.8.1/include/c++/4.8.1/bits/basic_string.tcc:996:5: note: template<class _CharT, class _Traits, class _Alloc> std::basic_istream<_CharT, _Traits>& std::operator>>(std::basic_istream<_CharT, _Traits>&, std::basic_string<_CharT, _Traits, _Alloc>&)
operator>>(basic_istream<_CharT, _Traits>& __in,
^
/usr/local/gcc-4.8.1/include/c++/4.8.1/bits/basic_string.tcc:996:5: note: template argument deduction/substitution failed:
main.cpp:18:16: note: mismatched types 'std::basic_istream<_CharT, _Traits>' and 'int'
items[i] >> cin;
^
In file included from /usr/local/gcc-4.8.1/include/c++/4.8.1/istream:879:0,
from /usr/local/gcc-4.8.1/include/c++/4.8.1/iostream:40,
from main.cpp:1:
/usr/local/gcc-4.8.1/include/c++/4.8.1/bits/istream.tcc:955:5: note: template<class _CharT2, class _Traits2> std::basic_istream<_CharT, _Traits>& std::operator>>(std::basic_istream<_CharT, _Traits>&, _CharT2*)
operator>>(basic_istream<_CharT, _Traits>& __in, _CharT* __s)
^
/usr/local/gcc-4.8.1/include/c++/4.8.1/bits/istream.tcc:955:5: note: template argument deduction/substitution failed:
main.cpp:18:16: note: mismatched types 'std::basic_istream<_CharT, _Traits>' and 'int'
items[i] >> cin;
^
In file included from /usr/local/gcc-4.8.1/include/c++/4.8.1/istream:879:0,
from /usr/local/gcc-4.8.1/include/c++/4.8.1/iostream:40,
from main.cpp:1:
/usr/local/gcc-4.8.1/include/c++/4.8.1/bits/istream.tcc:923:5: note: template<class _CharT, class _Traits> std::basic_istream<_CharT, _Traits>& std::operator>>(std::basic_istream<_CharT, _Traits>&, _CharT&)
operator>>(basic_istream<_CharT, _Traits>& __in, _CharT& __c)
^
/usr/local/gcc-4.8.1/include/c++/4.8.1/bits/istream.tcc:923:5: note: template argument deduction/substitution failed:
main.cpp:18:16: note: mismatched types 'std::basic_istream<_CharT, _Traits>' and 'int'
items[i] >> cin;
^
In file included from /usr/local/gcc-4.8.1/include/c++/4.8.1/iostream:40:0,
from main.cpp:1:
/usr/local/gcc-4.8.1/include/c++/4.8.1/istream:727:5: note: template<class _Traits> std::basic_istream<char, _Traits>& std::operator>>(std::basic_istream<char, _Traits>&, unsigned char&)
operator>>(basic_istream<char, _Traits>& __in, unsigned char& __c)
^
/usr/local/gcc-4.8.1/include/c++/4.8.1/istream:727:5: note: template argument deduction/substitution failed:
main.cpp:18:16: note: mismatched types 'std::basic_istream<char, _Traits>' and 'int'
items[i] >> cin;
^
In file included from /usr/local/gcc-4.8.1/include/c++/4.8.1/iostream:40:0,
from main.cpp:1:
/usr/local/gcc-4.8.1/include/c++/4.8.1/istream:732:5: note: template<class _Traits> std::basic_istream<char, _Traits>& std::operator>>(std::basic_istream<char, _Traits>&, signed char&)
operator>>(basic_istream<char, _Traits>& __in, signed char& __c)
^
/usr/local/gcc-4.8.1/include/c++/4.8.1/istream:732:5: note: template argument deduction/substitution failed:
main.cpp:18:16: note: mismatched types 'std::basic_istream<char, _Traits>' and 'int'
items[i] >> cin;
^
In file included from /usr/local/gcc-4.8.1/include/c++/4.8.1/iostream:40:0,
from main.cpp:1:
/usr/local/gcc-4.8.1/include/c++/4.8.1/istream:774:5: note: template<class _Traits> std::basic_istream<char, _Traits>& std::operator>>(std::basic_istream<char, _Traits>&, unsigned char*)
operator>>(basic_istream<char, _Traits>& __in, unsigned char* __s)
^
/usr/local/gcc-4.8.1/include/c++/4.8.1/istream:774:5: note: template argument deduction/substitution failed:
main.cpp:18:16: note: mismatched types 'std::basic_istream<char, _Traits>' and 'int'
items[i] >> cin;
^
In file included from /usr/local/gcc-4.8.1/include/c++/4.8.1/iostream:40:0,
from main.cpp:1:
/usr/local/gcc-4.8.1/include/c++/4.8.1/istream:779:5: note: template<class _Traits> std::basic_istream<char, _Traits>& std::operator>>(std::basic_istream<char, _Traits>&, signed char*)
operator>>(basic_istream<char, _Traits>& __in, signed char* __s)
^
/usr/local/gcc-4.8.1/include/c++/4.8.1/istream:779:5: note: template argument deduction/substitution failed:
main.cpp:18:16: note: mismatched types 'std::basic_istream<char, _Traits>' and 'int'
items[i] >> cin;
^